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?
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.
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.
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.
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.
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.
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.
You can use adapter.snapshot().items
. More info can be found here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With