LoaderManager get data offline, then online

╄→гoц情女王★ 提交于 2019-12-06 16:13:23

问题


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?


回答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).



来源:https://stackoverflow.com/questions/10856308/loadermanager-get-data-offline-then-online

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!