4

There are several tutorials on how to implement custom AsyncTaskLoaders but none I found discusses how to handle data caching - in all of them the data are just loaded and delivered.

Can you give me some hints how to implement custom AsyncTaskLoader that would return cached versions of data immediatelly but at the same time request an update of the data and deliver the updated vesions of them as soon as they arrive? Is this even possible with Loaders or do I have to use another mechanism and which?

Background: I have an IntentService which accepts requests for data loading and delivers the results in broadcasts. I have an UI (ListView) that should display that data from ArrayAdapter or CrusorAdapter. I need some "caching" layer that would keep lastest known versions of the data to be displayed immediatelly but issue their update and re-display them as soon as new data arrives.

Although this could be done using dumb SQLite database for immediate versions and broadcast receiver for new data update, I'd give a preference to something that would make this "backgorund" data loading mechanism transparent like Loader-derived class IMHO could be.

Blackhex
  • 1,694
  • 3
  • 21
  • 45
  • cache is a datastorage. If high amount of data is to be stored == databases. If low amount == sharedpreferences. Or do you only wan't to cache per session? – Anders Metnik Aug 21 '12 at 13:02
  • Yes, SQLite database would be the best option for cached data storage in my case but that's not the question. Question is about data fetching to UI synchronization mechanism (e.g. with AsyncTaksLoader derived class) while some cache must be taken into account. – Blackhex Aug 21 '12 at 13:05
  • For example this tutorial http://neilgoodman.net/2011/12/26/modern-techniques-for-implementing-rest-clients-on-android-4-0-and-below-part-1/ touches the topic but it states: "I won't be discussing how to cache fetched data in a SQLite database, but I will probably come back to that in a future blog post." which unfortunatelly didn't happen. – Blackhex Aug 21 '12 at 13:10
  • Ahh sorry, misunderstood ya then. – Anders Metnik Aug 21 '12 at 13:11

1 Answers1

-1

Yes, AsycnTask is the best way to do this. All you would do is write to your cache on the onPostExecute() of the AsyncTask. This could be done by creating a time stamp that compares data times and if there is a new time, use that data. Otherwise, you would have to compare all the data, and that can take precious time and resources, so it would depend on how much data you had.

Here is a link that might help:

http://developer.android.com/training/efficient-downloads/redundant_redundant.html

BlackHatSamurai
  • 23,275
  • 22
  • 95
  • 156
  • Hmm, I think I didn't make myself clear to you too. Although AsyncTask is another parallelism mechanism that could be used in this case, the problem of delivering the data to the UI remains. Basically, I'm asking in which method of AsyncTaskLoader (don't confuse with AsyncTask which is lower-level tool) should I deliver Cursor with old cached versions (to the CursorAdapter connected to my UI ListView) and in which method should I deliver Cursor with updated data. – Blackhex Aug 21 '12 at 13:34
  • Maybe two-level AsyncTask could be used: First AsyncTask starts another AsyncTask, queries the SQLite database, updates the UI using Handler or runOnUiThread() method and then finishes. While second AsyncTask downloads the data and updates the UI in the same way. But this solution seems quite ackward to me and it could bring more problems with synchronization that its benefits are. – Blackhex Aug 21 '12 at 13:38
  • 1
    I think you are over complicating it. `AsycnTask` runs on the background thread, and the `onPostExecute` runs on the UI. So you could download with `AsyncTask` and then use `onPostExecute` to update the UI. If you are trying update data as it is being downloaded, you could use the `onUpdate` method. – BlackHatSamurai Aug 21 '12 at 15:23
  • You mean `onProgressUpdate()`? In that case first call of `publishProgress()` could deliver cached versions of the data, next `publishProgress()` calls could deliver ListView rows one-by-one as they arrives and then `onPostExecute()` would just notify that list is completly up-to-date? That could work. Thanks! – Blackhex Aug 21 '12 at 15:31