Search code examples
androidandroid-paging-3

At the very 1st scroll it is laggy while using paging library 3


I have created a simple sample where I used paging3 jetpack library in it : https://github.com/alirezaeiii/Paging3-Sample

At the very 1st scroll it is laggy, and I can not figure out why. Would you please help out?

private const val STARTING_PAGE_INDEX = 1

class ShowPagingSource(
    private val service: ShowService
) : PagingSource<Int, Show>() {

    override suspend fun load(params: LoadParams<Int>): LoadResult<Int, Show> {
        val page = params.key ?: STARTING_PAGE_INDEX
        return try {
            val response = service.fetchShowList(page)
            LoadResult.Page(
                data = response,
                prevKey = if (page == STARTING_PAGE_INDEX) null else page - 1,
                nextKey = if (response.isEmpty()) null else page + 1
            )
        } catch (exception: IOException) {
            LoadResult.Error(exception)
        } catch (exception: HttpException) {
            LoadResult.Error(exception)
        }
    }
}

In my repository layer :

@Singleton
class ShowRepositoryImpl @Inject constructor(private val service: ShowService) : ShowRepository {

    override val fetchResultStream: Flow<PagingData<Show>> = Pager(
        config = PagingConfig(pageSize = NETWORK_PAGE_SIZE),
        pagingSourceFactory = { ShowPagingSource(service) }
    ).flow


    companion object {
        const val NETWORK_PAGE_SIZE = 245
    }
}

And in the ViewModel class :

@HiltViewModel
class ShowViewModel @Inject constructor(
    repository: ShowRepository
) : ViewModel() {

    val pagingDataFlow: Flow<PagingData<Show>> =
        repository.fetchResultStream.cachedIn(viewModelScope)
}

In my Activity I submit the data in recyclerView as follow :

lifecycleScope.launch {
            viewModel.pagingDataFlow.collectLatest(showAdapter::submitData)
        }

Is there something missed? why scrolling is laggy in 1st scroll?


Solution

  • I reported it to issue tracker as a minor bug : https://issuetracker.google.com/issues/238227615?pli=1