0

How can you tell if paging is working properly? All the examples I've looked at involve using retrofit apiservice which appears to be returning pages of data, but I'm pulling down a single rss feed and parsing it into a giant List<POJO>. I suspect that my PagingSource is loading the entire list into one page, but I'm not sure how to tell.

My list has near 1000 items, so I assume it'd be good practice to implement some kind of paging/DiffUtil. I'm playing around in this with jetpack compose usingandroidx.paging:paging-compose:1.0.0-alpha12 which probably complicates things.

Can anyone give me some pointers?

class RssListSource(): PagingSource<Int, RssItem>() {
    override suspend fun load(params: LoadParams<Int>): LoadResult<Int, RssItem> {
        return try {

            val nextPage = params.key ?: 1
            val rssList: List<RssItem> = RssFeedFetcher().fetchRss()

            LoadResult.Page(
                data = rssList,
                prevKey = if (nextPage == 1) null else nextPage - 1,
                nextKey = nextPage.plus(1)
            )
        } catch (e: Exception){
            LoadResult.Error(e)
        }
    }
}
class MainActivityViewModel: ViewModel() {
    val rss: Flow<PagingData<RssItem>> = Pager(PagingConfig(pageSize = 10)){
        RssListSource()  // returned to LazyPagingItems list in @Composable 
    }.flow.cachedIn(viewModelScope)
}
dbarnes
  • 439
  • 1
  • 3
  • 11

3 Answers3

0

Your data still needs a way to fetch pages of data. I would expect your RssFeedFetcher to use the page information and return a page accordingly.

You are probably correct that you are currently returning all items at once.

Kilian
  • 275
  • 2
  • 8
0

There's two main strategies here:

  1. Add a long enough delay() to load() such that you have enough time to scroll to the end of the list before new page loads
class RssListSource(): PagingSource<Int, RssItem>() {
    override suspend fun load(params: LoadParams<Int>): LoadResult<Int, RssItem> {
  delay(5000)
  ...
}
  1. Observe changes to LazyPagingItems.loadState and look for either PREPEND or APPEND switching between Loading and NotLoading.
dlam
  • 3,547
  • 17
  • 20
0

In order for you to be able to implement pagination with the paging library you need to use a paginated API, that means that in your example, you'd need a way to fetch the RSS in a paginated fashion. The Paging library won't be able to make use of pagination if your data source does not provide a way to query for paginated data, unfortunately.

You could achieve what you want by implementing a middleware that fetches the RSS feed and splits it into pages for you to query from the Android app.

EDIT: Another approach could be to have a background task (using the Android WorkManager) to download the RSS feed and save it in a Room Database, then use the Paging library to load pages off the database. Here's a summary of how to show paginated data from a Room database: https://developer.android.com/topic/libraries/architecture/paging/v3-network-db

Xavi Rigau
  • 1,421
  • 13
  • 17