LoaderManager get data offline, then online
Asked Answered
B

1

0

I would like to follow this nice usability pattern, where app stores data offline for faster response and updates it when it gets new data online.

And I use Loaders with LoaderManager.

Now, what is the correct approach to implement the aforementioned approach with Loaders?

Currently I use two approaches, which have their downsides and, generally, are not very elegant.

  1. Storing the data in the application context instead of SQLite
  2. Two separate AsyncTaskLoaders - offlineLoader and onlineLoader. The first fetches the data from SQLite database and shows it immediately if it's there and the second one loads the data from external server, writes it to database (for the offlineLoader) and replaces the offline data

What I would really like to have is:

A single Loader which fetches the data offline and returns it to LoaderManager, but continues to work afterwards and return the fresh online data when it's ready. So it should return twice - is it even possible?

Beattie answered 1/6, 2012 at 19:34 Comment(1)
Now that I thought about it, it should be the matter of overriding onDeliverResult() to start downloading online data... Somebody tell me if I'm wrong, but I'll try with this approachSarmentose
B
1

The solution is to use ContentProviders with Loaders loading data to the UI and Services downloading data and pushing it into the database (and notifying the ContentProvider and the rest of the app, that it's been changed).

Beattie answered 2/6, 2012 at 22:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.