Skip to main content

How should a SQLiteDatabase connection be handled inside an AsyncTaskLoader?



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?


Comments

  1. The best way would probably be to take a copy of CursorLoader.java and modify the loadInBackground() method to use your SQLiteDatabase

    /* 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;
    }

    ReplyDelete

Post a Comment

Popular posts from this blog

Slow Android emulator

I have a 2.67 GHz Celeron processor, 1.21 GB of RAM on a x86 Windows XP Professional machine. My understanding is that the Android emulator should start fairly quickly on such a machine, but for me it does not. I have followed all instructions in setting up the IDE, SDKs, JDKs and such and have had some success in staring the emulator quickly but is very particulary. How can I, if possible, fix this problem?