1

In Android paging library, how do I invalidate the subset of the dataset in a PagedList or a DataSource? I write PagedList or DataSource because I don't know which one I should call.

For example, I'm making a QA App called BufferOverflow.

I have a PageKeyedDataSource that load data over the network. It support pagination by the index on the API. In that App, I have a Top Question screen that has "infinite" list of top questions. Of course, the top question is updated periodically.

How to:

  1. Update the dataset at page 1?
  2. Update the dataset at page 10?

I understand that the benefit of using PagedList is it will do a calculation on which item is new/old, using DiffUtil. But, how do I tell it the dataset on certain key or position has changed?

Is what I'm asking impossible? Should I implement custom paging behaviour on my own?


Note:

  1. I don't want to invalidate all the data.

adapter.getCurrentList().getDataSource().invalidate();

This will invalidate the whole data on data source. While I just want to invalidate certain part of the data.

  1. I think API that I'm looking for is similar to RecyclerView.Adapter's

list.adapter.notifyItemChanged(0);

But of course, its not Adapter's responsibility to update the dataset. It's either on the PagedList, DataSource, or Dao classes..

aldok
  • 17,295
  • 5
  • 53
  • 64
  • `"I don't want to invalidate all the data."` - you have to, there is no other way - for more see [this](https://developer.android.com/reference/android/arch/paging/DataSource) and find `Updating Paged Data` section – pskink Feb 09 '19 at 05:30
  • Why not invalidate the data and properly implement `loadBefore`/`loadAfter`? – EpicPandaForce Feb 09 '19 at 08:33
  • @EpicPandaForce would you explain what do you mean by properly implement `loadBefore` / `loadAfter`? – aldok Feb 09 '19 at 08:50
  • so why dont you want to invalidate all the data? – pskink Feb 09 '19 at 09:20
  • @pskink because then that would be inefficient. The reason I'm using paging is because I thought we only need to invalidate small portion of data to reload the list (the diff will be calculated by DiffUtil). For example, in Twitter App, we don't have to reload all the Tweet, but only invalidate the first index of the Tweet (new Tweets). – aldok Feb 09 '19 at 10:34
  • @pskink You're right. PagedList is immutable. If I wanted to update small portion of data, I have to implement multiple DataSource's, and get update information from new PagedList. Thanks for sharing the documentation link :) – aldok Feb 09 '19 at 11:20
  • Did you success?@aldok – milad salimi Jun 14 '20 at 11:35

1 Answers1

0

The only way to solve this issue is to introduce a persistence layer with Room. https://github.com/googlesamples/android-architecture-components/tree/master/PagingWithNetworkSample#paging-with-database-and-network

As per docs here: https://developer.android.com/reference/androidx/paging/DataSource#updating-paged-data

If you have more granular update signals, such as a network API signaling an update to a single item in the list, it's recommended to load data from the network into memory. Then present that data to the PagedList via a DataSource that wraps an in-memory snapshot. Each time the in-memory copy changes, invalidate the previous DataSource, and a new one wrapping the new state of the snapshot can be created.

https://developer.android.com/topic/libraries/architecture/paging/data#consider-content-updates

As you construct observable PagedList objects, consider how content updates work. If you're loading data directly from a Room database updates get pushed to your app's UI automatically.

Badhrinath Canessane
  • 3,408
  • 2
  • 24
  • 38