Java Android分页库:如何在在线和离线数据之间智能切换?
我正在关注Raywenderlich关于如何使用android分页库的教程。这是网上最简单的教程之一,我已经完全遵循了它。但是,我想做一些更改,以便能够智能地在在线数据和离线数据之间切换 也就是说,我的数据库中有一些旧帖子。最初我有互联网连接。所以我从互联网上加载最新的数据,然后将其插入我的数据库。最后,我在我的recyclerView/PagedListAdapter中显示这个最新数据。 如果由于某种原因,有一段时间后没有互联网连接,我应该显示数据库中的旧帖子 我该怎么做 我的尝试: 这是我的 在这里,我尝试创建一个工厂模式。它检查最初我是否有互联网,工厂从在线数据源返回pagedList。否则,工厂将从脱机数据源返回pagedList。 但这并不能智能地在这两种状态之间切换 我尝试了一些随机代码,比如创建边界回调。但我不知道如何做出必要的改变。 我不是在这里添加代码(至少现在是这样)来保持它的简短和精确 有人能帮我吗 编辑:Java Android分页库:如何在在线和离线数据之间智能切换?,java,android,kotlin,android-paging,android-paging-library,Java,Android,Kotlin,Android Paging,Android Paging Library,我正在关注Raywenderlich关于如何使用android分页库的教程。这是网上最简单的教程之一,我已经完全遵循了它。但是,我想做一些更改,以便能够智能地在在线数据和离线数据之间切换 也就是说,我的数据库中有一些旧帖子。最初我有互联网连接。所以我从互联网上加载最新的数据,然后将其插入我的数据库。最后,我在我的recyclerView/PagedListAdapter中显示这个最新数据。 如果由于某种原因,有一段时间后没有互联网连接,我应该显示数据库中的旧帖子 我该怎么做 我的尝试: 这是我的
具体来说,我主要从网络加载分页数据。如果存在网络错误,我不想向用户显示错误。相反,我从缓存/数据库加载分页数据,并尽可能长时间地持续向用户显示。如果网络已恢复,请切换回网络分页数据。(我就是这么想的instagram/facebook)。实施这一目标的适当方式是什么?在答案中查看我的代码/尝试。好的,在试用了两天的一些代码后,这就是我想到的。然而,我真的不知道这是不是一个好的做法。因此,我愿意接受任何可以接受的答案 说明: 因为我有多个数据源(网络和数据库),所以我创建了
ProfilePostDataSource:PageKeyedDataSource
这里的密钥是一对,第一个用于网络分页,第二个用于数据库分页
我使用kotlin的协同程序以一种简单的方式编写了一些异步代码。因此,我们可以在psudo代码中编写它,如下所示:
Database db;
Retrofit retrofit;
inside loadInitial/loadBefore / loadAfter:
currNetworkKey = params.key.first;
currDBKey = params.key.second;
ArrayList<Model> pagedList;
coroutine{
ArrayList<Model> onlineList = retrofit.getNetworkData(currNetworkKey); // <-- we primarily load data from network
if(onlineList != null) {
pagedList = onlineList;
db.insertAll(onlineList); // <-- update our cache
}else{
ArrayList<Model> offlineList = db.getOfflineData(currDBKey); // <-- incase the network fails, we load cache from database
if(offlineList !=null){
pagedList = offlineList;
}
}
if(pagedList != null or empty) {
nextNetworkKey = // update it accordingly
nextDBKey = // update it accordingly
Pair<int, int> nextKey = new Pair(nextNetworkKey, nextDBKey);
pagingLibraryCallBack.onResult(pagedList, nextKey); // <-- submit the data to paging library via callback. this updates your adapter, recyclerview etc...
}
}
数据库数据库;
改造;
内部loadInitial/loadBefore/loadAfter:
currentworkkey=params.key.first;
currDBKey=params.key.second;
ArrayList页面列表;
协同程序{
ArrayList onlineList=Reformation.getNetworkData(currNetworkKey);//0){
nextDBKey=cachedList.last().id;
}否则{
nextDBKey=无效的\u密钥;
}
nextNetworkKey=INVALID_KEY;//下一个解决方案可能是:如果您只需要在设备连接时从API中获取数据,请始终从API中获取数据,如果您收到未知的HostException或IOException,则从数据中获取数据我现在看到了您的代码,因此您正在使用带调用的改装,因此您获得了成功和成功失败侦听器方法。如果接收到throwable参数,您需要检查throwable类型,如果合适,然后从数据库中获取数据。为此,您需要通过构造函数注入数据库。您可以在存储库中查看下一种改型方法和流程(有关方法的一些提示)您链接的教程看起来已经在这样做了。基本上是数据源。由Room生成的工厂将始终从DB/缓存的脱机数据加载,并触发BoundaryCallback以从网络获取项目。这意味着所有分页都由本地缓存数据驱动,而本地缓存数据由BoundaryCallback从网络增量更新。Wh您在实现BoundaryCallback时遇到了哪些问题?如果您有一些具体的问题,我可以尝试回答。您是否计划在遇到网络错误时切换顺序,并希望优先从数据库中加载?BoundaryCallback不直接获取要显示的项,它将其存储在数据库中,然后使分页无效,以让分页拾取这些项e新的项目,所以它已经实现了这一点,没有任何额外的代码。要清楚,如果您遇到的问题是在数据库中有过时的数据,我会简单地清除数据库,只要你想刷新。
/** @brief: <Key, Value> = <Integer, ProfilePost>. The key = pageKey used in api. Value = single item data type in the recyclerView
*
* We have a situation. We need a 2nd id to fetch profilePosts from database.
* Change of plan: <Key, Value> = < Pair<Int, Int>, ProfilePost>. here the
*
* key.first = pageKey used in api. <-- Warning: Dont switch these 2!
* Key.second = db last items id
* used as out db page key
*
* Value = single item data type in the recyclerView
*
* */
class ProfilePostDataSource: PageKeyedDataSource<Pair<Long, Long>, ProfilePost> {
companion object{
val TAG: String = ProfilePostDataSource::class.java.simpleName;
val INVALID_KEY: Long = -1;
}
private val context: Context;
private val userId: Int;
private val liveLoaderState: MutableLiveData<NetworkState>;
private val profilePostLocalData: ProfilePostLocalDataProvider;
public constructor(context: Context, userId: Int, profilePostLocalData: ProfilePostLocalDataProvider, liveLoaderState: MutableLiveData<NetworkState>) {
this.context = context;
this.userId = userId;
this.profilePostLocalData = profilePostLocalData;
this.liveLoaderState = liveLoaderState;
}
override fun loadInitial(params: LoadInitialParams<Pair<Long, Long>>, pagingLibraryCallBack: LoadInitialCallback<Pair<Long, Long>, ProfilePost>) {
val initialNetworkKey: Long = 1L; // suffix = networkKey cz later we'll add dbKey
var nextNetworkKey = initialNetworkKey + 1;
val prevNetworkKey = null; // cz we wont be using it in this case
val initialDbKey: Long = Long.MAX_VALUE; // dont think I need it
var nextDBKey: Long = 0L;
GlobalScope.launch(Dispatchers.IO) {
val pagedProfilePosts: ArrayList<ProfilePost> = ArrayList(); // cz kotlin emptyList() sometimes gives a weird error. So use arraylist and be happy
val authorization : String = AuthManager.getInstance(context).authenticationToken;
try{
setLoading();
val res: Response<ProfileServerResponse> = getAPIService().getFeedProfile(
sessionToken = authorization, id = userId, withProfile = false, withPosts = true, page = initialNetworkKey.toInt()
);
if(res.isSuccessful && res.body()!=null) {
pagedProfilePosts.addAll(res.body()!!.posts);
}
}catch (x: Exception) {
Log.e(TAG, "Exception -> "+x.message);
}
if(pagedProfilePosts.isNotEmpty()) {
// this means network call is successfull
Log.e(TAG, "key -> "+initialNetworkKey+" size -> "+pagedProfilePosts.size+" "+pagedProfilePosts.toString());
nextDBKey = pagedProfilePosts.last().id;
val nextKey: Pair<Long, Long> = Pair(nextNetworkKey, nextDBKey);
pagingLibraryCallBack.onResult(pagedProfilePosts, prevNetworkKey, nextKey);
// <-- this is paging library's callback to a pipeline that updates data which inturn updates the recyclerView. There is a line: adapter.submitPost(list) in FeedProfileFragment. this callback is related to that line...
profilePostLocalData.insertProfilePosts(pagedProfilePosts, userId); // insert the latest data in db
}else{
// fetch data from cache
val cachedList: List<ProfilePost> = profilePostLocalData.getProfilePosts(userId);
pagedProfilePosts.addAll(cachedList);
if(pagedProfilePosts.size>0) {
nextDBKey = cachedList.last().id;
}else{
nextDBKey = INVALID_KEY;
}
nextNetworkKey = INVALID_KEY; // <-- probably there is a network error / sth like that. So no need to execute further network call. thus pass invalid key
val nextKey: Pair<Long, Long> = Pair(nextNetworkKey, nextDBKey);
pagingLibraryCallBack.onResult(pagedProfilePosts, prevNetworkKey, nextKey);
}
setLoaded();
}
}
override fun loadBefore(params: LoadParams<Pair<Long, Long>>, pagingLibraryCallBack: LoadCallback<Pair<Long, Long>, ProfilePost>) {} // we dont need it in feedProflie
override fun loadAfter(params: LoadParams<Pair<Long, Long>>, pagingLibraryCallBack: LoadCallback<Pair<Long, Long>, ProfilePost>) {
val currentNetworkKey: Long = params.key.first;
var nextNetworkKey = currentNetworkKey; // assuming invalid key
if(nextNetworkKey!= INVALID_KEY) {
nextNetworkKey = currentNetworkKey + 1;
}
val currentDBKey: Long = params.key.second;
var nextDBKey: Long = 0;
if(currentDBKey!= INVALID_KEY || currentNetworkKey!= INVALID_KEY) {
GlobalScope.launch(Dispatchers.IO) {
val pagedProfilePosts: ArrayList<ProfilePost> = ArrayList(); // cz kotlin emptyList() sometimes gives a weird error. So use arraylist and be happy
val authorization : String = AuthManager.getInstance(context).authenticationToken;
try{
setLoading();
if(currentNetworkKey!= INVALID_KEY) {
val res: Response<ProfileServerResponse> = getAPIService().getFeedProfile(
sessionToken = authorization, id = userId, withProfile = false, withPosts = true, page = currentNetworkKey.toInt()
);
if(res.isSuccessful && res.body()!=null) {
pagedProfilePosts.addAll(res.body()!!.posts);
}
}
}catch (x: Exception) {
Log.e(TAG, "Exception -> "+x.message);
}
if(pagedProfilePosts.isNotEmpty()) {
// this means network call is successfull
Log.e(TAG, "key -> "+currentNetworkKey+" size -> "+pagedProfilePosts.size+" "+pagedProfilePosts.toString());
nextDBKey = pagedProfilePosts.last().id;
val nextKey: Pair<Long, Long> = Pair(nextNetworkKey, nextDBKey);
pagingLibraryCallBack.onResult(pagedProfilePosts, nextKey);
setLoaded();
// <-- this is paging library's callback to a pipeline that updates data which inturn updates the recyclerView. There is a line: adapter.submitPost(list) in FeedProfileFragment. this callback is related to that line...
profilePostLocalData.insertProfilePosts(pagedProfilePosts, userId); // insert the latest data in db
}else{
// fetch data from cache
// val cachedList: List<ProfilePost> = profilePostLocalData.getProfilePosts(userId);
val cachedList: List<ProfilePost> = profilePostLocalData.getPagedProfilePosts(userId, nextDBKey, 20);
pagedProfilePosts.addAll(cachedList);
if(pagedProfilePosts.size>0) {
nextDBKey = cachedList.last().id;
}else{
nextDBKey = INVALID_KEY;
}
nextNetworkKey = INVALID_KEY; // <-- probably there is a network error / sth like that. So no need to execute further network call. thus pass invalid key
val nextKey: Pair<Long, Long> = Pair(nextNetworkKey, nextDBKey);
pagingLibraryCallBack.onResult(pagedProfilePosts, nextKey);
setLoaded();
}
}
}
}
private suspend fun setLoading() {
withContext(Dispatchers.Main) {
liveLoaderState.value = NetworkState.LOADING;
}
}
private suspend fun setLoaded() {
withContext(Dispatchers.Main) {
liveLoaderState.value = NetworkState.LOADED;
}
}
}