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.

developer
  • 189
  • 2
  • 6

4 Answers4

14

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().

nayan dhabarde
  • 1,734
  • 1
  • 19
  • 38
4

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();
    }
kike
  • 4,255
  • 5
  • 23
  • 41
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)});
Almighty
  • 835
  • 8
  • 16
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)