Paging3 When Room data changed

Viewed 51

DataSource.class

@NonNull
@Override
public ListenableFuture<LoadResult<Integer, AccountBookItemReListParent>> loadFuture(@NonNull LoadParams<Integer> loadParams) {
    Integer nextPageNumber = loadParams.getKey();
    if (nextPageNumber == null) {
        nextPageNumber = 1; //Start read from page 1
    }
    int finalNextPageNumber = nextPageNumber;
    ListenableFuture<LoadResult<Integer, AccountBookItemReListParent>> future = Futures.transform(executorService.submit(new Callable<List<AccountBookItemReListParent>>() {
        @Override
        public List<AccountBookItemReListParent> call() {
            if (finalNextPageNumber == 0) return getAccountBookItemsToReList(new ArrayList<>());
            int[] startEnd = calculatePageRange(finalNextPageNumber, loadParams.getLoadSize());
            return getAccountBookItemsToReList(database.getAccountBookItemDAO().getAccountItemsByParentToListLIMIT(parentBookId, startEnd[0], startEnd[1]));
        }
    }), accountBookItems -> {
        return new LoadResult.Page<>(accountBookItems,
                finalNextPageNumber == 0 ? null : finalNextPageNumber - 1, accountBookItems.isEmpty() ? null : finalNextPageNumber + 1);
    }, executorService);

    ListenableFuture<LoadResult<Integer,AccountBookItemReListParent>> partialLoadResultFuture = Futures.catching(
            future, Exception.class,
            LoadResult.Error::new, executorService);

    return Futures.catching(partialLoadResultFuture,
            Exception.class, LoadResult.Error::new, executorService);
}

ViewModel

public LiveData<PagingData<AccountBookItemReListParent>> getPagingAccoungBookItems() {
    CoroutineScope coroutineScope = ViewModelKt.getViewModelScope(this);
    Pager<Integer, AccountBookItemReListParent> pager = new Pager<>(pagingConfig,
            () -> new AccountBookItemLiveDataSource(dataBaseX,currentAccountBook.getId()));
    return PagingLiveData.cachedIn(PagingLiveData.getLiveData(pager), coroutineScope);
}

Observe:

ViewModel.getPagingAccoungBookItems().observe(getInstance(), new Observer<PagingData<AccountBookItemReListParent>>() {
        @Override
        public void onChanged(PagingData<AccountBookItemReListParent> accountBookItemPagingData) {
            accountBookItemViewAdapter.submitData(getLifecycle(), accountBookItemPagingData);
        }
    });

I'm using Paging3. And I used DAO to modify the database, and how to make the content of paging displayed in recyclerview change together. Although I could use Adapter.refresh(); to refresh, But when the data reaches hundreds will case a jam. I'd like to know how to make it fluently?

0 Answers
Related