1

I'm using a LoaderManager, the onCreateLoader method creates a new CursorLoader.

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle data) {
return new CursorLoader(context, CONTENT_URI, PROJECTION,
                null, null, null);
}

Only after loading is finished in onLoadFinished() the UI is updated. Every time I load the cusor I also need to do some database manipulation like building up a new sort index. This cannot be done asynchronously because the UI depends on this.

Is there a way to do such db operation within the loader? Or what is the best design for this problem?

Naveen
  • 109
  • 8
  • "Is there a way to do such db operation" what operations are you referring to? – pskink Apr 06 '15 at 20:21
  • writing to the database instead of reading. I think I can move this task in the ContentProvider prior to fetching the data. – Naveen Apr 06 '15 at 21:04

2 Answers2

0

As per your question you are loading your data in from database using loader and content provider

also you are working on sorting type of thing which can change the order of item in database...

So, the best way I suggest as per my experience in this type of application of do this of index sorting operation in UI only util user leave the screen...

So, In you activity of fragment override onStop method and update data indexes in database based on sorting priorities or numbers...

and after updating data to content provider just notify URI for change..

hope my point is clear to you..

jignesh.world
  • 1,396
  • 1
  • 11
  • 26
  • 1
    I have 2 views on my data: an overview activity and a detailed activity where the user can interact. Writing to the database in onstop in the detailed view would make data inconsistent to what was previous displayed when the user restarts. Therefore I need to do this at startup of the overview activity. – Naveen Apr 06 '15 at 20:50
0

Loaders were designed specifically with optimizing database access in mind. This operation does not care about updating the UI and hence has no interest in providing progress information. While it is possible (and I use this loosely), to update the UI from a loader, you should avoid this as the Loader is a wrong tool for this job. Forcing a Loader to provide progress information would break the paradigm. The Loader is expected to return only after the etire operation is complete. Instead, if you want to update the UI while doing the querying, then you should use an AsyncTask.

If you HAVE to use a Loader, then you can find a workaround here at Update progressbar from AsyncTaskLoader?. But again, since from your question, it looks like you are open to alternatives, use the AsyncTask if you need updates or you can stick to Java threads.

Community
  • 1
  • 1
ucsunil
  • 7,378
  • 1
  • 27
  • 32