문제

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

/* 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;
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top