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:
PagingSourcerepresents a source of paged data. Room can be set up to return a
@Daofunction, for example.
PagingDatarepresents a chunk of data retrieved from the underlying data source via the
Pagerrepresents a specific operation for getting paged data out of a
PagingSource. We can teach a
Pagerhow many items to retrieve in a “page”, and the
Pagergives us a
LiveData, or similar reactive API for getting
PagingDataobjects as needed.
RecyclerView.Adapterthat knows how to work with a
PagingData. As the user scrolls the
PagingDataAdaptercan signal that we need more data, triggering the
Pagerto fetch more data from the
PagingSourceand emit a fresh
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.