使用RxJava时,在分页3库中进行刷新

11
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实例,但重复使用了一个PagingSource实例。确保传递给Pager的pagingSourceFactory始终返回一个新的PagingSource实例。

这是我用来获取数据的可流对象。


你找到解决方案了吗? - Volodymyr Zakharov
不,我改用了普通的分页方法。你试过下面的答案吗? - developer
4个回答

15

因此,与其

val pagingSource = PagingSource()

return Pager() {
   pagingSource
}.flow

请使用此功能


return Pager() {
   PagingSource()
}.flow

基本上,它有助于存储分页资源的引用,稍后适配器在调用刷新(refresh())时使用它来使其失效。


5
您需要使用InvalidatingPagingSourceFactory (链接)。它包含了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
            ); 
    
            // inti Flowable
            pagingDataFlow = PagingRx.getFlowable(pager);
            return pagingDataFlow;
        }


 public void invalidatePageSource() {
        invalidatingFactory.invalidate();
    }

2

我曾经遇到过类似的问题,后来发现了这个问题,帮助了我。

Pager的分页源工厂lambda表达式必须始终返回源的新实例。在您的情况下,它始终返回您在init()方法中创建的实例。

尝试将其更改为以下内容:

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

0

应该创建新的pagingSource实例

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

如果一切都不起作用,也许可以尝试使用分页的3.0.0-alpha01版本(不太推荐)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接