Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Paging 3 - get list of data from PagingData<T> object

I'm using new jetpack Paging 3 library. I have one specific use case. I want to share this data between two screens using viewmodel.

One screen needs paged data and for the second screen I want simple list of this data (not paged list, I need to work with the list...). I do not want list in object of PagingData.

Is there any way how to get the list without PagingData object ? enter image description here

As you can see there is no variable the get this data. I tried it even in viewModel when flow is created but I haven't found solution.

like image 838
P.Dudik Avatar asked Dec 02 '20 16:12

P.Dudik


People also ask

How can I get data from PagingData?

Display the paged data in your UI Create an instance of your PagingDataAdapter class. Pass the PagingDataAdapter instance to the RecyclerView list that you want to display your paged data. Observe the PagingData stream, and pass each generated value to your adapter's submitData() method.

Which API defines the source of data to be retrieved from a single source in paging?

Repository layer Each PagingSource object defines a source of data and how to retrieve data from that source. A PagingSource object can load data from any single source, including network sources and local databases. Another Paging library component that you might use is RemoteMediator .

What is pagingdata object in Android?

A PagingData object is a container for a snapshot of paginated data. It queries a PagingSource object and stores the result. The primary Paging library component in the UI layer is PagingDataAdapter, a RecyclerView adapter that handles paginated data.

What is the paging 3 library?

The Paging 3 library, a part the new set of Android Jetpack libraries, provides a robust way of paginating large sets of data in Android whether it is loaded from a room database or from a network layer. The library provides 3 different utilities for loading paginated data:

How to define a data source in paging 3?

Unlike the previous versions of Paging library, in Paging 3.0 we have to implement a PagingSource<Key, Value> to define a data source. The PagingSource takes two parameters a Key and a Value. The Key parameter is the identifier of the data to be loaded such as page number and the Value is the type of the data itself.

Is it possible to retrieve the list of data of pagingdata?

To expand on dlam 's comment, it is possible to retrieve the list of data of PagingData, as long as it is connected to a PagingDataAdapter, or if the PagingData result is submitted to a AsyncPagingDataDiffer. if connected to a AsyncPagingDataDiffer.


Video Answer


2 Answers

PagingData is just a stateless stream of incremental load events, so it does not hold this kind of state.

However PagingDataAdapter / Differ (and similarly other presenter-side variants), already need to hold the data, so they expose APIs such as PagingDataAdapter.snapshot() which can give you the current list of presented items.

Keep in mind that the presented data is post-transformation, may not include pages that were dropped due to exceeding maxSize, and also may race with fetcher as it takes some time between loading the data, and having it show up in UI.

It really depends on what you're interested in tracking exactly, but from the fetcher side (tracking pre-transform) you can either build the list from results returned in PagingSource or you can also write a no-op .map() operator on PagingData which will see every item (but not give you information about order).

Unfortunately Paging3 doesn't yet offer a non-ui collector that can build this state for you (most useful in testing), but this is hopefully something we can look into in the future.

like image 183
dlam Avatar answered Oct 23 '22 14:10

dlam


To expand on dlam's comment, it is possible to retrieve the list of data of PagingData, as long as it is connected to a PagingDataAdapter, or if the PagingData result is submitted to a AsyncPagingDataDiffer.

To do that, invoke

adapter.snapshot().items

if connected to a PagingDataAdapter, or

differ.snapshot().items

if connected to a AsyncPagingDataDiffer.

like image 2
1stlulu Avatar answered Oct 23 '22 14:10

1stlulu