问题
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.
- Storing the data in the application context instead of SQLite
- Two separate
AsyncTaskLoaders
-offlineLoader
andonlineLoader
. 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?
回答1:
The solution is to use ContentProvider
s with Loader
s loading data to the UI and Service
s downloading data and pushing it into the database (and notifying the ContentProvider
and the rest of the app, that it's been changed).
来源:https://stackoverflow.com/questions/10856308/loadermanager-get-data-offline-then-online