Using a Cursor returned from a LoaderManager in an

2019-04-29 06:00发布

问题:

I have a cursor returned on an onLoadFinished callback (from LoaderManager.LoaderCallbacks), and I want to do some (possibly costly) post-processing on this cursor. So, I'm firing off an AsyncTask that uses this Cursor. However, I'm getting intermittent crashes with this exception:

android.database.StaleDataException: Attempted to access a cursor after it has been closed.

My suspicion is that this is happening because the cursor (being managed by the Loader in the UI Thread) is being closed before the background thread is finished with it, since this is a managed cursor. Here is some paraphrased code:

private class LoaderListener implements LoaderManager.LoaderCallbacks<Cursor> {
    @Override
    public void onCreateLoader(int d, Bundle args) {
        return new CursorLoader(context, uri, projection, selection, selectionArgs, sortOrder);
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
        processCursor(cursor)    
    }
}

private void processCursor(final Cursor cursor) {
    new AsyncTask<Void, Void, Result> {
        @Override
        Result doInBackground(Void... params) {
            while(cursor.isAfterLast() == false) {
                // doing some costly things with cursor
            }
        }
    }.execute();
}

Is it possible to either,

  1. Somehow flag the cursor to prevent it from being closed from the UI thread.

  2. Notify the manager that the cursor is still in use.

  3. Clone it so that the cloned instance doesn't get closed by the manager.

  4. Another, even better, solution?

Having this post-processing done on the UI thread is absolutely not an option, however, as it can be very costly.

回答1:

Is it possible to somehow flag the cursor to prevent it from being closed from the UI thread?

No (well, not without re-writing the internal APIs).

Is it possible to notify the manager that the cursor is still in use?

Same answer as above.

Is it possible to clone it so that the cloned instance doesn't get closed by the manager?

This sounds kind of messy... and there is still the chance that the LoaderManager closes the cursor before you are able to finish cloning it.

Is there a better solution?

Yes. Query a new cursor instead of trying to reusing the one that you pass to the LoaderManager.



回答2:

Man, I am facing the very same problem. The way is to cancel the asynctask in the activity's onDestroy method.

private YourAsyncTask asyncTask

@Override
protected void onDestroy(){
    super.onDestroy();
    asyncTask.cancel(true);
}