Paging (Arch. Comp.)
The Android Paging Library helps you load and display small data chunks at a time. Learn to use ObjectBox database with the Paging library from Android Architecture Components.
Since 2.0.0
ObjectBox supports integration with the Paging library that is part of Google's Android Architecture Components. To that end, the ObjectBox Android library (objectbox-android
) provides the ObjectBoxDataSource
class. It is an implementation of the Paging library's PositionalDataSource
.
Note: the following assumes that you have already added and set up the Paging library in your project.
Using ObjectBoxDataSource
Within your ViewModel
, similar to creating a LiveData
directly, you first build your ObjectBox query. But then, you construct an ObjectBoxDataSource
factory with it instead. This factory is then passed to a LivePagedListBuilder
to build the actual LiveData
.
Here is an example of a ViewModel
class doing just that:
Note that the LiveData
holds your entity class, here Note
, wrapped inside a PagedList
. You observe the LiveData
as usual in your activity or fragment, then submit the PagedList
on changes to your PagedListAdapter
of the Paging library.
We will not duplicate how this works here, see the Paging library documentation for details about this.
Next steps
Have a look at the ObjectBox Architecture Components example code.
Check out ObjectBox support for LiveData.
Learn how to build queries.
Last updated