【问题标题】:Refresh in paging 3 library when using RxJava使用 RxJava 时在分页 3 库中刷新
【发布时间】:2021-03-25 16:29:33
【问题描述】:
homeViewModel.pagingDataFlow.subscribe(expensesPagingData -> {
                expenseAdapter.submitData(getLifecycle(), expensesPagingData);    
            }, throwable -> Log.e(TAG, "onCreate: " + throwable.getMessage()));

// ViewModel

 private void init() {
        pagingDataFlow = homeRepository.init();
        CoroutineScope coroutineScope = ViewModelKt.getViewModelScope(this);
        PagingRx.cachedIn(pagingDataFlow, coroutineScope);

    }

// Repository 
    public  Flowable<PagingData<ExpensesModel>> init() {
            // Define Paging Source
            expensePagingSource = new ExpensePagingSource(feDataService);
            // Create new Pager
            Pager<Integer, ExpensesModel> pager = new Pager<Integer, ExpensesModel>(
                    
                    new PagingConfig(10,
                            10,
                            false, 
                            10, 
                            100),
                    () -> expensePagingSource); // set paging source
    
            // inti Flowable
            pagingDataFlow = PagingRx.getFlowable(pager);
            return pagingDataFlow;
        }


I have tried to invalidate still not working.
 public void invalidatePageSource() {
        if (expensePagingSource != null) {
            expensePagingSource.invalidate();
        }
    }

当使用expenseAdapter.refresh() 刷新适配器时 当 Pager 期望创建一个新实例时,一个 PagingSource 实例被重新使用。确保传递给 Pager 的 pagingSourceFactory 始终返回一个新的 PagingSource 实例。 这是我用来获取数据的可流动对象。

任何帮助将不胜感激。

【问题讨论】:

  • 找到解决方案了吗?
  • 不行,我切换到正常的分页方式,你试过下面的答案吗?

标签: android android-recyclerview android-paging-library android-paging-3


【解决方案1】:

所以不是

val pagingSource = PagingSource()

return Pager() {
   pagingSource
}.flow

使用这个


return Pager() {
   PagingSource()
}.flow

基本上,它有助于存储分页资源的引用,稍后当您调用 refresh() 时,适配器会使用该引用对其调用无效。

【讨论】:

    【解决方案2】:

    我的代码也有类似的问题,我发现这个 issue 对我有帮助。

    寻呼机的寻呼源工厂 lambda 必须始终返回源的新实例。在您的情况下,它总是返回您在 init() 方法中创建的实例。

    尝试将其更改为:

    Pager<Integer, ExpensesModel> pager = new Pager<Integer, ExpensesModel>(
                    
                    new PagingConfig(10,
                            10,
                            false, 
                            10, 
                            100),
                    () -> {new ExpensePagingSource(feDataService)});
    

    【讨论】:

      【解决方案3】:

      应该创建新的 pagingSource 实例

      Pager(
            config = PagingConfig(...),
            pagingSourceFactory = {
                      PagingSource()// new instance
                  }
            ).flow.cachedIn(viewModelScope)
      

      如果没有任何效果,可以尝试 3.0.0-alpha01 版本的分页(不那么推荐)

      【讨论】:

        【解决方案4】:

        您需要使用InvalidatingPagingSourceFactory (link)。它包含 invalidate() 方法,该方法使已调度的每个 PagingSource 无效。

        在你的代码中有这样的东西:

            // Repository 
            InvalidatingPagingSourceFactory invalidatingFactory = 
                InvalidatingPagingSourceFactory {
                    new ExpensePagingSource(feDataService);
                }
            public  Flowable<PagingData<ExpensesModel>> init() {
                    // Create new Pager
                    Pager<Integer, ExpensesModel> pager = new Pager<Integer, ExpensesModel>(
                            
                            new PagingConfig(
                                    10,
                                    10,
                                    false, 
                                    10, 
                                    100),
                                    invalidatingFactory); // set paging source
            
                    // inti Flowable
                    pagingDataFlow = PagingRx.getFlowable(pager);
                    return pagingDataFlow;
                }
        
        
         public void invalidatePageSource() {
                invalidatingFactory.invalidate();
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-12-29
          • 2020-02-12
          • 2012-10-24
          • 2015-03-18
          相关资源
          最近更新 更多