首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android分页库:如何在线上线下数据之间智能切换?

Android分页库:如何在线上线下数据之间智能切换?
EN

Stack Overflow用户
提问于 2020-09-28 18:56:22
回答 1查看 1.2K关注 0票数 0

我在paging-library-for-android-with-kotlin上跟随Raywenderlich关于如何使用android分页库的教程。这是网络上最简单的教程之一,我已经完全了解了它。但是,我想进行一些更改,以便能够智能地在在线数据和离线数据之间进行切换。

也就是说,我的数据库中有一些旧的帖子。最初我有网络连接。所以我从互联网上加载最新的数据,然后将其插入我的数据库中。最后,在我的recyclerView / PagedListAdapter中显示这个最新数据。如果由于某些原因,有一段时间后,没有互联网连接,我应该从数据库中显示旧帖子。

我该怎么做呢?

我的尝试:

这是我的code on github repository

在这里,我尝试创建一个工厂模式。它检查我最初是否有互联网,工厂是否从online dataSource返回pagedList。ELse,则工厂将从脱机dataSource返回pagedList。但这并不能智能地在两种状态之间切换。

我尝试了一些随机代码,比如创建边界回调。但我不确定如何做出必要的改变。我不会在这里添加代码(至少现在),以保持简短和精确。

有谁可以帮我?

编辑:

具体地说,我主要从网络加载分页数据。如果有网络错误,我不想向用户显示错误。相反,我从缓存/数据库加载分页数据,并尽可能长时间地将其持续显示给我的用户。如果网络恢复,请切换回网络分页数据。(我认为instagram和facebook就是这么做的)。实现这一点的适当方式是什么?请参阅答案中的我的代码/尝试。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-09-29 20:15:16

好的,在尝试了两天的代码之后,这就是我想出来的。然而,我真的不知道这是不是一个好的实践。因此,我对任何可接受的答案都持开放态度。

解释:

因为我有多个数据源(网络和数据库),所以我在这里创建了ProfilePostDataSource: PageKeyedDataSource<Pair<Long, Long>, ProfilePost>,键是一对,第一个用于网络分页,第二个用于数据库分页。

我使用kotlin的协程以一种简单的if-else方式编写了一些异步代码。所以我们可以用伪代码来编写它,如下所示:

代码语言:javascript
复制
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...
    }
  }

因此,在facebook、instagram等应用程序中,我们看到他们主要从网络加载数据。但如果网络出现故障,他们会向你显示一个现金数据。我们可以像下面的代码一样智能地实现这个开关。

下面是一个相关的代码片段,用kotlin编写的PageKeyedDataSource:

ProfilePostDataSource.kt

代码语言:javascript
复制
/** @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;
    }
  }

}

感谢你读到这里。如果您有更好的解决方案,请随时让我知道。我对任何可行的解决方案都持开放态度。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64100844

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档