With the increase in the number of users of a particular mobile application, there is an increase in the amount of data associated with that application. For example, with the increase in the number of users of the Instagram app, the daily feeds of the app also increased to a large amount.
In general, if we want to display some data from our remote server to our Android application, we fetch the data at the time of launching of that particular activity that will display the data and after that, we show the data on that activity. But think of the Instagram application. With 1+ billion users, you can’t even imagine the amount of data that the Instagram application has. And if the app starts loads the data from the server and then displays the data on our feeds then it will take years to load all the data from the server.
So, what we want is, if the user comes to the bottom of the screen then we can fetch some more data from the server and display it. But what if the user at the same time scrolls up the screen? So, you need to handle a lot of cases while using this approach. To avoid these kinds of difficulties and to load only a desired amount of data from the database, Google introduced the concept of Paging in Android.
In this blog, we will learn about Paging and will use this concept in our Android Application. So, let’s get started.
What is Paging?
Paging is a part of the Android Jetpack and is used to load and display small amounts of data from the remote server. By doing so, it reduces the use of network bandwidth. Some of the advantages of Paging can be:
1. You will get the content on the page faster
2. It uses very less memory
3. It doesn’t load the useless data i.e. only one or two pages can be loaded at a time.
4. You can use Paging along with LiveData and ViewModel to observe and update data in an easier way.
Components of Paging
Following are the components of Paging:
DataSource: DataSource is the class where you tell your application about how much data you want to load in your application. You can load the data from the server by using three subclasses of the DataSource class. They are:
1. ItemKeyedDataSource: If you want the data of an item with the help of the previous item then you can use ItemKeyedDataSource. For example, while commenting to a post, you need the id of the last comment to get the content of the next comment.
2. PageKeyedDataSource: If your feeds page has some feature of next or previous post then you can use PageKeyedDataSource. For example, you can see the next post by clicking the next button in any social media platform.
3. PositionalDataSource: If you want to fetch data only from a specific location then you can use PositionalDataSource. For example, out of 1000 users, if you want to fetch the data of the users from 300 to 400 then you can do this by using PositionalDataSource.
PagedList: By using the PagedList class, you can load the data of your application. If more and more data are added or need by the user then that extra data will be added to the previous PagedList only. Also, if there is a change in the data is observed then a new instance of PagedList is emitted to the observer with the help of the LiveData or RxJava.
So, to use the LiveData holder of PagedList objects in your ViewModel, you can use the below code:
classConcertViewModel(concertDao: ConcertDao) : ViewModel() {
valconcertList: LiveData
concertDao.concertsByDate().toLiveData(pageSize = 30)
}
New Data: With the help of PagedListAdapter, whenever a new page is loaded by the PagedList then the PagedListAdapter will notify the RecyclerView that a new data has been added and the RecyclerView will update that data to the UI.