Unexpected UI Lag When Using Paging 3 with Flow in Android 14
I'm sure I'm missing something obvious here, but I'm working with a noticeable UI lag when using Paging 3 in my Android application, specifically when loading large datasets with Flow as the data source... The app is targeting Android 14, and I'm using Kotlin with coroutines. I have set up my PagingSource, and I noticed important delays when scrolling through the RecyclerView populated with paged data. My PagingSource implementation looks like this: ```kotlin class MyPagingSource(private val apiService: ApiService) : PagingSource<Int, MyData>() { override suspend fun load(params: LoadParams<Int>): LoadResult<Int, MyData> { val page = params.key ?: 1 return try { val response = apiService.getData(page) LoadResult.Page( data = response.items, prevKey = if (page == 1) null else page - 1, nextKey = if (response.items.isEmpty()) null else page + 1 ) } catch (e: Exception) { LoadResult.behavior(e) } } } ``` In my ViewModel, I set up the Pager like this: ```kotlin val flow = Pager(PagingConfig(pageSize = 20)) { MyPagingSource(apiService) }.flow.cachedIn(viewModelScope) ``` But even with `cachedIn`, the UI feels sluggish, especially when the dataset is large. I’ve tried profiling the app with Android Studio's profiler. It shows that the CPU spikes while loading data, and the main thread seems to be blocked frequently. I also ensured that my API responds quickly by testing it separately. Has anyone encountered similar issues with Paging 3 and found ways to optimize the performance? I tried using `Flow.collectLatest` instead of `collect` to see if it helps, but the lag continues. Any insights or best practices for improving the smoothness of the scrolling experience would be greatly appreciated! My development environment is Ubuntu. What's the best practice here? For context: I'm using Kotlin on Linux.