Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can we get entire object list passes to the PagingDataAdapter while using Paging library 3.0?

I am new to the paging library 3.0, I need to paginate data only from the local database. Initially, I fetched PagingSource<Int, MessageDisplayModel> from the database and use it as flow.

fun getChatMessages(chatWrapper: ChatWrapper): Flow<PagingData<MessageDisplayModel>> {
    return Pager(
            config = PagingConfig(pageSize = 15,
                    maxSize = 50,
                    enablePlaceholders = true)
    ) {
        xmppChatManager.getChatMessages(chatWrapper)
    }.flow
}

Then after PagingData is passing to the adapter through submitData() method.

lifecycleScope.launch {
        @OptIn(ExperimentalCoroutinesApi::class)
        mViewModel.getChatMessages(chatWrapper).collectLatest {
            adapter.submitData(it) }
    }

Now, my concern is how can we get the actual list of MessageDisplayModel from PagingData which I pass to the adapter?

like image 403
AaNeal O'Neal Avatar asked Jul 06 '20 04:07

AaNeal O'Neal


People also ask

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.

How does Paging help with sharing of libraries?

The Paging Library lets you load data directly from your backend using keys that the network provides. Your data can be uncountably large. Using the Paging Library, you can load data into pages until there isn't any data remaining. You can observe your data more easily.

How does the pagingdata object work?

The PagingData object queries data from the PagingSource in response to loading hints that are generated as the user scrolls in a RecyclerView. Loads the data from GithubService, ensuring that multiple requests aren't triggered at the same time. Keeps an in-memory cache of the retrieved 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.

What is the return type of a pagingdataadapter?

PagingData: This is the final return type and something that PagingDataAdapter understands and has the original data type inside it. It acts as a paging data container.

What is the paging 3 library?

This concludes the implementation of the Paging 3 library which guided you to consume large data sources over the network. The paging library also has a feature to work with SQLite database to show data while having a database cache. This functionality is not production-ready yet.


1 Answers

You can use adapter.snapshot().items. More info can be found here.

like image 60
n1k3c Avatar answered Oct 18 '22 14:10

n1k3c