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.
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 -
- PageKeyed
- ItemKeyed
- PositionKeyed
There are 3 case to load data to list.
- Live from server
- Caching into the database and periodically loading from server
- Loading only from database
Here we need 3 components.
- PageKeyedDataSource (Can use item or position keyed data source too as per use case)
- DataSource.Factory
- PagedListAdapter
- 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.
This is used to instanciate the page keyed datasource.
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.
Here we need 3 components.
- DataSource.Factory
- PagedListAdapter
- 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.
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.
- 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.
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.
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