I wondering where and when I should open/close my SQLiteDatabase connection in my AsyncTaskLoader. I don't feel like i fully understand the lifecycle of a Loader, so I'm afraid that I might run into some memory leaks/NullPointerExceptions if I don't to this right. I currently open up my SQLiteDatabase in the constructor of my loader:
private class SQLiteCursorLoader extends AsyncTaskLoader<Cursor> {
private String _queryString;
private SQLiteDatabase _db;
...
public SQLiteCursorLoader(Context context, String queryString) {
super(context);
_queryString = queryString;
_db = MySQLiteOpenHelper.getWritableDatabase();
}
....
}
But where do I close the connection again?
The best way would probably be to take a copy of CursorLoader.java and modify the loadInBackground() method to use your SQLiteDatabase
ReplyDelete/* Runs on a worker thread */
@Override
public Cursor loadInBackground() {
Cursor cursor = // Your stuff goes here <..>
if (cursor != null) {
// Ensure the cursor window is filled
cursor.getCount();
registerContentObserver(cursor, mObserver);
}
return cursor;
}