Question

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.

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top