Skip to content

Paging Library

Devrath edited this page Jun 20, 2021 · 50 revisions

Contents of the paging library
Introduction
Recyclerview Introduction
Issue with a large number of data sets in recycler view
How paging helps in this
Paging elements
Data source types
Issues associated with earlier way of endless loading
Using paging instead of endless list functionality
Using Paging with -> Local + Remote

Introduction

  • Our world has become overloaded with data.
  • data include things like news feed, video, music and more...
  • When things keep increasing we need to keep in mind not to overload the device itself because the device has limited number of resources.
  • Phone has a very limited amount of real estate available to show the data that is available.

Recyclerview Introduction

* On this limited amount of real estate, displaying the data in an efficient manner was solved by the usage of the `view holder pattern` in android. * Instead of created `n-views` for `n-number` of data, using the recycler view we could create few views for `n-number` of data and recycle the views as we scroll along. * We can see how this is done in the diagram above.

Issue with a large number of data sets in recycler view

  • The view sure is recycled and helps in smooth scrolling but the data set is still large this can be handled efficiently.
  • But still the data is in memory say a list. If the data set increases to tens and thousands of items, we may run into performance issues.
  • This performance issue is for sure in older android devices.

How paging helps in this

  • Paging library helps in fixing this issue in the data side by displaying only the data needed currently on the screen display.
  • What view holder does on the view side, the paging does on the data side
  • Paging library doesn't keep all the data in the memory at once - > Paging library pulls the data from the data source on an as-needed basis.
  • Paging library is designed to particularly operate well on live data and room database

Paging elements

Paging element Description
Data Source Determines where data comes from, like if it is room database or a network
PageList It is like a window to the list of items
PageList Adapter It is a subclass of recycler view, that is used to show paged list in recycler views

Data source types

Data source type Description
Page Keyed Data Source It gives paged items at a time
Item Keyed Data Source It gives items one by one
Positional Data Source Any number of items from any position

Issues associated with earlier way of endless loading

  1. We need to keep track of keys so that we can retrieve the next and previous pages.
  2. We need to request the correct next page when the user scrolls at the end of the recycler view.
  3. We need to prevent duplicate requests.

Using paging instead of endless list functionality

  • Paging library takes care of all the three points in the issues mentioned.
  • Paging keeps track of the loading state, and if there is an error in loading, we can show a view with retry view.
  • Refreshing of the list is also just a method call involved.
  • We can transform the data using the list transformations using operators like filter, map etc which can be flow or rx-java.
  • Adding the list separators is also very much easier.

Using Paging with -> Local + Remote

Blocks of paging component
Data Source
Using the paging source
Challenges faced without a paging library

Data Source

  • Data source is a single source of data - it's the single source of truth.
  • If you are loading the data from a single source, Be it from file-system, remote-source, local-database, we implement the paging source
  • If we are using the room, it implements the paging source to help you.
  • If you are fetching the data from the server and storing it locally finally displaying the data to the end-user, We use remote-mediator that handles the data from the server and maintains a cached local state.

Using the paging source

  • To use the paging source we need to extend the PagingSource class and override the load method which is a suspend function.
  • Since this is a suspend function, we can use other suspend functions inside this like getting data from local database, remote-api, etc.
  • Here we can pass the prev-page, next-page keys received from the server.
  • If we don't receive the keys and the API is index-based, we just use calculate it here.
  • Here we return data along with keys wrapped in load-result, And we return the error if there is an error occurred.
  • Because of this when you build a network request, the keys are provided.

Challenges faced without a paging library

Clone this wiki locally