Rest API Pagination with Paging Library.

Almost every REST API your app call requires to handle pagination. When calling an REST API for some resource instead of delivering all of the results, which could be time-consuming and cumbersome to deal with, a REST API will typically make you paginate through the results.If you are trying to support pagination on a client you need to handle it gracefully.

Let’s say that there’s an API endpoint that returns a list of users. In the database of the API, there are 100,000+ users. It would be impractical to fetch all 100,000 users from the API all in one request. In fact, we would probably get an OutOfMemory exception. To avoid this, we want to paginate through our list of the user when making requests to our API.

Rest Api Pagination with Paging Library

Paging Library


The new paging library makes it easier for your app to gradually load information as needed from a REST API, without overloading the device or waiting too long for an all result.This library contains several classes to streamline the process of requesting data as you need it. These classes also work seamlessly with existing architecture components, like Room.

1.Adding Components to your Project


Architecture Components are available from Google’s Maven repository. To use them, follow these steps:

Open the build.gradle file for your project and add the line as shown below:

Add Architecture Components

In this tutorial, we are using LiveData, and ViewModel.

Open the build.gradle file for your app or module and add the artifacts that you need as dependencies:

2.Setting up Retrofit for Pagination


The code samples below for making the API calls are using Retrofit with GSON.We are working with the GitHub API and making a call to the GitHub User endpoint.

Now, let’s write our class that will generate our RetrofitService.

3.Create TiledDataSource


UseTiledDataSource class to define a data source.It uses for incremental data loading, used in list paging. You can load arbitrary pages based solely on the position information and can provide a fixed item count. TiledDataSource supports querying pages at arbitrary positions, so can provide data to PagedLists in arbitrary order.

To implement, extend TiledDataSourcesubclass.

4.Create ViewModel


In the ViewModel, we would extend from the Architecture Component ViewModel, and then we would keep a reference to that LiveData of our PagedList.

LivePagedListProvider: It is use for construct Database that return LiveData.
PagedList: A PagedList is a List which loads its data in chunks (pages) from a DataSource.All data in a PagedList is loaded from its DataSource. Creating a PagedList loads data from the DataSource immediately, and should, for this reason, be done on a background thread. The constructed PagedList may then be passed to and used on the UI thread. This is done to prevent passing a list with no loaded content to the UI thread, which should generally not be presented to the user.

5.Create Adapter


To tell the PagedListAdapter how to compute the difference between the two elements, you’ll need to implement a new class, DiffCallback.Here, you will define two things.

You will define how to compute whether the contents are the same, and how to define whether the items are the same.
Let’s look at the adapter.So our adapter would extend PagedListAdapter, and then it will connect the user, which is the information that’s being displayed, with the user ViewHolder.

We define the callback, the DIFF_CALLBACK, for our user objects and then in onBindViewHolder, all we need to do is bind the item to the ViewHolder.

6.Create RecyclerView


In the onCreate, we get the reference to our ViewModel and get the reference to the RecyclerView, and we create our adapter.

 

Download this project

 

Related Post

Architecture Components:Paging Library.

 

 

One Reply to “Rest API Pagination with Paging Library.”

  1. Is possible to add progressFooterView in recyclerview when loading data is slow (i.e Iternet connection is low level)???

Leave a Reply

Your email address will not be published. Required fields are marked *