Unexpected UI Lag When Using RecyclerView with Paging 3 in Android 14
I'm learning this framework and I've encountered a strange issue with I'm working on a project and hit a roadblock. I've been banging my head against this for hours. I'm experiencing important UI lag when scrolling through a `RecyclerView` that utilizes Paging 3 in my Android 14 app. The lag seems to occur specifically when the dataset is large (over 10,000 items). I've implemented the `PagingSource` and am observing that the data loading is taking longer than expected, which is causing a noticeable delay in the UI. Iām using `ViewModel` to manage the UI-related data and `LiveData` to observe it. Here's a simplified version of my `PagingSource` implementation: ```kotlin class MyPagingSource : PagingSource<Int, MyItem>() { override suspend fun load(params: LoadParams<Int>): LoadResult<Int, MyItem> { val page = params.key ?: 1 return try { val response = myApiService.getItems(page) LoadResult.Page( data = response.items, prevKey = if (page == 1) null else page - 1, nextKey = if (response.items.isEmpty()) null else page + 1 ) } catch (exception: Exception) { LoadResult.behavior(exception) } } } ``` And here's how I'm setting it up in my `ViewModel`: ```kotlin class MyViewModel : ViewModel() { val items = Pager(PagingConfig(pageSize = 20)) { MyPagingSource() }.flow.cachedIn(viewModelScope) } ``` In my Fragment, I observe the `items` LiveData and submit it to the adapter: ```kotlin myViewModel.items.observe(viewLifecycleOwner) { pagingData -> myAdapter.submitData(lifecycle, pagingData) } ``` I've debugged the API response times, and they're within acceptable limits, so I suspect the scenario might be related to how the `RecyclerView` handles the data. I've also tried increasing the `pageSize` to see if it helps, but the lag continues. Has anyone encountered similar performance optimization with RecyclerViews and Paging in Android 14? Any suggestions on how to troubleshoot or optimize the performance? Thanks in advance! Thanks in advance! This issue appeared after updating to Kotlin latest. What are your experiences with this? What's the best practice here?