How to use the SQLite database to store synchronization data from Web thread security

advertisements

I have an AsyncTask that fetches information from a web service and displays in the UI. This requires me to fetch the info every time the application starts, which wastes bandwidth and takes long for info to appear when you start the app.

I would like to store the fetched information in a local SQLite Database, and sync it every once in a while with the data from the web service.

What would be the best approach to do that?

If I create another AsyncTask for the database, and run it as soon as the app starts, I risk having a race condition where the database is updated as I'm reading information from it to display in the UI. What is recommended for this? I assume it's a common problem, though I wasn't able to find the right keywords to find an answer...

Thanks

Edit: My min API level is 8, so any solution should work with it or available in the support library.


If you would like to cache your data locally in a SQLite database, consider creating a Content Provider which provides a wrapper around the SQLite database and also allows you to use Loaders which can pull information from your database and automatically update your UI as the underlying data changes.