Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refresh in paging 3 library when using RxJava

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.

like image 938
developer Avatar asked Mar 22 '26 17:03

developer


1 Answers

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

like image 175
nayan dhabarde Avatar answered Mar 25 '26 07:03

nayan dhabarde



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!