3

I'm using Paging 3 (androidx.paging:paging-runtime-ktx:3.1.0) to load a list of items and it works fine except there is a delay in showing items on recyclerview after hiding a progress loading and after debug the steps I found the delay comes from submitData function on PagingDataAdapter , so how can I resolve this?

here is how I handle a load state

 viewLifecycleOwner.lifecycleScope.launch {
        adapter.loadStateFlow.collectLatest { loadState ->
            binding.rvShimmer.isVisible = loadState.refresh is LoadState.Loading
            if (loadState.refresh is LoadState.Error) {
                if ((loadState.refresh as LoadState.Error).error.message?.contains(Constants.NETWORK_ERROR) == true) {
                    showErrorView(ListError.NO_INTRERNET)
                } else {
                    showErrorView(ListError.UN_EXPECTED)
                }
                showTryAgainButton()
            } else if (loadState.source.refresh is LoadState.NotLoading && loadState.append.endOfPaginationReached && adapter.itemCount < 1) {
                showEmptyView()
            } else {
                isOrdersLoaded = true
                showRecyclerView()
            }
        }
    }

    binding.rv.adapter = adapter.withLoadStateHeaderAndFooter(
        header = PagingLoadingStateAdapter { adapter.retry() },
        footer = PagingLoadingStateAdapter { adapter.retry() },
    )

and here is how I submit data

viewLifecycleOwner.lifecycleScope.launch {
            viewModel.listData.collectLatest { data ->
                adapter.submitData(viewLifecycleOwner.lifecycle, data)
            }
        }
Amin
  • 463
  • 2
  • 11
  • 29
  • Facing the same issue when using the paging data from a room paging source. The delay does indeed come from the submit data function of the paging data adapter. On a side note, you should be using the non-blocking suspendable overload of submit data function, i.e., `adapter.submitData(data)`. – ashu Nov 13 '22 at 14:11

0 Answers0