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
PagingSourcerepresents a source of paged data. Room can be set up to return aPagingSourcefrom a@Query-annotated@Daofunction, for example. - A
PagingDatarepresents a chunk of data retrieved from the underlying data source via thePagingSource - A
Pagerrepresents a specific operation for getting paged data out of aPagingSource. We can teach aPagerhow many items to retrieve in a “page”, and thePagergives us aFlow,LiveData, or similar reactive API for gettingPagingDataobjects as needed. - A
PagingDataAdapteris aRecyclerView.Adapterthat knows how to work with aPagingData. As the user scrolls theRecyclerView, thePagingDataAdaptercan signal that we need more data, triggering thePagerto fetch more data from thePagingSourceand emit a freshPagingDatafor thePagingDataAdapterto 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.