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

2

11

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?

Dunker answered 6/7, 2020 at 4:33 Comment(0)
D
21

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

Dierdredieresis answered 16/9, 2020 at 15:2 Comment(1)
Is exist way to get items without adapter, directly from PagingData ?Polymerize
N
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

Narbonne answered 6/12, 2020 at 9:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.