Refresh in paging 3 library when using RxJava
Asked Answered
M

4

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();
        }
    }

When refreshing the adapter using expenseAdapter.refresh() An instance of PagingSource was re-used when Pager expected to create a newinstance. Ensure that the pagingSourceFactory passed to Pager always returns a new instance of PagingSource. This my floawable object which I am using to get data.

Any help will be appreciated.

Misvalue answered 25/3, 2021 at 16:29 Comment(2)
Did you find the solution?Incorporator
No, I switched to the normal pagination method, Have you tried the below answer?Misvalue
C
16

So instead of

val pagingSource = PagingSource()

return Pager() {
   pagingSource
}.flow

Use this


return Pager() {
   PagingSource()
}.flow

Basically it helps store the reference of paging resource which is later used by adapter to call invalidate on it when you call refresh().

Champerty answered 11/1, 2022 at 13:4 Comment(0)
E
6

You need to use the InvalidatingPagingSourceFactory (link). It contains the invalidate() method that invalidates every PagingSource that has been dispatched.

Something like this in your code:

    // 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();
    }
Exclamation answered 1/2, 2022 at 11:8 Comment(0)
D
2

I had similar problem with my code and I found this issue which helped me.

The paging source factory lambda of the Pager must always return a new instance of the source. In your case it's always returning the instance you created in your init() method.

Try to change it to something like:

Pager<Integer, ExpensesModel> pager = new Pager<Integer, ExpensesModel>(
                
                new PagingConfig(10,
                        10,
                        false, 
                        10, 
                        100),
                () -> {new ExpensePagingSource(feDataService)});
De answered 17/4, 2021 at 10:38 Comment(0)
C
0

should create new instance of pagingSource

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

if nothing works maybe try 3.0.0-alpha01 version of paging (not so recommended)

Coates answered 30/11, 2021 at 11:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.