Skip to content

Instantly share code, notes, and snippets.

@sagarnayak
Last active August 14, 2019 09:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sagarnayak/7bc9b02bddcff26d232d5646d1696616 to your computer and use it in GitHub Desktop.
Save sagarnayak/7bc9b02bddcff26d232d5646d1696616 to your computer and use it in GitHub Desktop.
Andoroid paging library Guide

Android Paging Library

This is a library in android jetpack to make the loading data to recyclerview easier and more efficient. It provies with different components like data source, list adapter and boundary callback to use it as per you use case and make you list loading easier.

Docs

DataSource Types

When we are using local Room database we can directly get datasource from the room DAO. but there are case when we need to create our own datasource. like loading the data directly from server. There are 3 types of datasource we can extend and use for loading data from srever -

  1. PageKeyed
  2. ItemKeyed
  3. PositionKeyed

There are 3 case to load data to list.

  1. Live from server
  2. Caching into the database and periodically loading from server
  3. Loading only from database

Loading data Directly from server

Here we need 3 components.

  1. PageKeyedDataSource (Can use item or position keyed data source too as per use case)
  2. DataSource.Factory
  3. PagedListAdapter

Why Use Them

PageKeyedDataSource

  • This is used to observe the list and load the data as per the requirement.
  • After extending this we need to override 3 methods loadInitial, loadAfter, loadBefore.
  • so when the list is being loaded for first time the load initial will be called and we need to pass the initial dataset at the callback.
  • and as the list is being scrolled the load before and after are called to provide the datasets to populate the list.

DataSource.Factory

This is used to instanciate the page keyed datasource.

PagedListAdapter

Previously we use the RecyclerVuew.Adapter for the recyclerview and to let the PageKeyedDataSource work properly we need to use the PagedListAdapter so that datasource can observe this and proper data can be populated.

Caching into the database and periodically loading from server

Here we need 3 components.

  1. DataSource.Factory
  2. PagedListAdapter
  3. BoundaryCalBack
  • When we are getting the data from the local database the data source changes. In online method we were using the pagekeyed datasource, but here we have to get the data source from database.
  • And if you are using Room database then it gives a Datasource.Factory which can be observed to pagedList of data.
  • so basically till now the change is pagekeyed datasource is replaced by DataSource.Factory from Room database.
  • the PagedListAdapter stays the same.
  • and the first problem we are going to face is that we do not have the initial data in database. so when to load that? and when to resync to server for more data? when to update the existing data in local database?
  • there is a class PagedList.BoundaryCallback made for that purpose. it will notify when the first page is being loaded into the list and when is the first and last part is being loaded into the list. so we can make a query to if the database requires to be synced.
  • the syncing of database is entirely the business logic and paging library does not do anything regarding it. it only hints you when what happens and accordingly we have to do the database work manually.

Observations

How to stop loading after last data from server

in PageKeyedDataSource we need to provide the adjecent page number in each callback and if any of page number is passed null then the next call will be not there from the PageKeyedDataSource. When to pass null is entirely the logic of how we get data we are getting from server.

Handling page loading error

  • if at any point the data fails to be provided to the list user needs to be notified with a proper error message.
  • in case of online loading directly post error from reopsitory to view through the viewmodel mediatorlivedata with the help of a mutablelivedata. and also handle the logical error internally.
  • same can be performed at the database caching case but have to be checked at the network level and at the database level.

Reinitialise / SwipeRefresh

when user does a swipe refresh the new fresh data should be loaded. but that is enrirely the business logic. but if you want to load the new data from server just call invalidate() at the DataSourceFactory in case of OnlineLoading or Delete Table Rows in case of local database caching.

Paged loading of data with database caching

when you are caching data to local database and server gives you paged data. it will be a prblem for you. as boundary callback does not give any kind of page number. it gives you first and the last item in the list. so better to use item paging from server where data is sent according to a item id. or you have to save the page number and other required data in you sharedpreference or somewhere to access later. Ref

Full Code Implementation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment