Group Group Group Group Group Group Group Group Group

Paging Library for Android With Kotlin: Creating Infinite Lists | Ray Wenderlich

In this tutorial, you will build up a simple Reddit Clone that loads pages of information gradually into an infinite list using the Paging library and Room.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/6948-paging-library-for-android-with-kotlin-creating-infinite-lists

You have an error in this code block when implementing Room:

  private fun initializedPagedListBuilder(config: PagedList.Config):
          LivePagedListBuilder<Int, RedditPost> {

    val livePageListBuilder = LivePagedListBuilder<Int, RedditPost>(
            database.postDao().posts(),
            config)
    return livePageListBuilder
  }

Should contain the line:

    val database = RedditDb.create(this)

I just checked the final code to see this missing bit

@alexsullivan Can you please help with this when you get a chance? Thank you - much appreciated! :]

Good catch! I’ve updated the tutorial accordingly.

A nice feature to add in this tutorial is to keep the recyclerView position after device rotation.

I’m managing this onSaveInstanceState and onResume() methods, but maybe there is a better solution with LiveData that I’m not aware.

@andrezizu That’s a good idea! I think your best bet is going to be exposing the LiveData from a jetpack ViewModel object. The ViewModel will then be saved on rotation and your list should display immediately.

Great post. How do you deal with stale data? When you restart the app you get a old copy of your data in the recycler view.

@alexsullivan Can you please help with this when you get a chance? Thank you - much appreciated! :]

@reinhardt1053 Good question. So, normally you’d use the invalidate method on the DataSource being utilized, but if you’ve followed the example project that will just force the paging library to refetch the data from your Room database, which will still be stale.

Instead, your best bet is going to be to interact directly with the database. For example - if you want to reload new data whenever the app starts, you could simply delete all of the posts from the RedditDb before setting up the paging library infrastructure (i.e. calling initializePagedListBuilder). That will force the paging library to fetch new content.

I hope that helps!

Thanks for your reply. I think an attempting to fetch fresh data should be made before deleting the stale data from the database. Otherwise if we delete the data when the application start we can’t use the app offline, which was the reason the only reason to use the database as cache layer in the first place.

Good point! You could start your fetch and then replace the items in the database with the new items. You’d effectively be circumnavigating the onZeroItemsLoaded method.

This tutorial is more than six months old so questions are no longer supported at the moment for it. Thank you!