Enter the Paging Library
The Paging library exists to provide greater developer control over exactly what gets loaded from a backing data store and when, handling things like:
- Performing smaller queries, to stay inside a
CursorWindow
’s bounds, so we can control the threads used for data loads - Supporting multiple traversal options through a data set: not only classic position-based systems, but ones where you might be navigating a tree and need to retrieve related child objects as part of traversal
- Offering reactive approaches, based on
LiveData
, so we can ensure that our UI remains responsive.
The Paging library has evolved over the years. The current edition, known as Paging v3, has a powerful Kotlin-centric API, and Room has basic support for returning Paging v3-based responses from queries.
There are a number of classes involved in the Paging library, but for basic scenarios, there are a few of significance:
- A
PagingSource
represents a source of paged data. Room can be set up to return aPagingSource
from a@Query
-annotated@Dao
function, for example. - A
PagingData
represents a chunk of data retrieved from the underlying data source via thePagingSource
- A
Pager
represents a specific operation for getting paged data out of aPagingSource
. We can teach aPager
how many items to retrieve in a “page”, and thePager
gives us aFlow
,LiveData
, or similar reactive API for gettingPagingData
objects as needed. - A
PagingDataAdapter
is aRecyclerView.Adapter
that knows how to work with aPagingData
. As the user scrolls theRecyclerView
, thePagingDataAdapter
can signal that we need more data, triggering thePager
to fetch more data from thePagingSource
and emit a freshPagingData
for thePagingDataAdapter
to use.
That still leaves a lot of moving parts, which we will examine in greater detail in this chapter.
Prev Table of Contents Next
This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.