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)
}
}