10

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?

Anil Bhomi
  • 691
  • 7
  • 12

2 Answers2

20

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

n1k3c
  • 203
  • 2
  • 6
0

as of paging 3 need constructor for insitating entire list of item to load once in your layout for binding if get list of pages by using snapshot() you cant update holder itemcount it's run before bindviewholder and DiffUtil also has high order lifecycle

try to use custom constructor

Nitish Kumar
  • 111
  • 3