ObjectBox Docs
HomeBlogTwitterGitHub
  • ObjectBox Docs
  • Getting started
  • Tutorial: Demo Project
  • Entity Annotations
  • Android (Java/Kotlin)
    • Android Local Unit Tests
    • LiveData (Arch. Comp.)
    • Paging (Arch. Comp.)
    • App Bundle, split APKs and LinkageError
    • greenDAO Compat
  • Desktop Apps
  • Kotlin Support
  • ObjectBox Queries
  • On-Device Vector Search
  • Data Observers & Rx
  • Relations
  • ObjectBox Admin
  • Transactions
  • Advanced
    • Advanced Setup
    • Object IDs
    • Custom Types
    • Entity Inheritance
    • Data Model Updates
    • Meta Model, IDs, and UIDs
  • Changelogs
  • Java API reference
  • Dart API reference
  • Python API reference
  • Binary License
  • FAQ
  • Troubleshooting
  • Data Sync
  • Swift Database for iOS
  • C++ Database Docs
  • Java Release History (<= v1.5)
Powered by GitBook
On this page
  • Using ObjectBoxDataSource
  • Next steps

Was this helpful?

Export as PDF
  1. Android (Java/Kotlin)

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.

PreviousLiveData (Arch. Comp.)NextApp Bundle, split APKs and LinkageError

Last updated 6 years ago

Was this helpful?

Since 2.0.0

ObjectBox supports integration with the that is part of Google's . To that end, the (objectbox-android) provides the ObjectBoxDataSource class. It is an implementation of the Paging library's .

Note: the following assumes that you have already in your project.

Using ObjectBoxDataSource

Within your ViewModel, similar to , you first . 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:

public class NotePagedViewModel extends ViewModel {
    
    private LiveData<PagedList<Note>> noteLiveDataPaged;
    
    public LiveData<PagedList<Note>> getNoteLiveDataPaged(Box<Note> notesBox) {
        if (noteLiveDataPaged == null) {
            // query all notes, sorted a-z by their text
            Query<Note> query = notesBox.query().order(Note_.text).build();
            // build LiveData
            noteLiveDataPaged = new LivePagedListBuilder<>(
                    new ObjectBoxDataSource.Factory<>(query),
                    20 /* page size */
            ).build();
        }
        return noteLiveDataPaged;
    }
}

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.

Next steps

We will not duplicate how this works here, see the for details about this.

Have a look at the .

Check out .

Learn how to .

Paging library
Android Architecture Components
ObjectBox Android library
PositionalDataSource
added and set up the Paging library
creating a LiveData directly
build your ObjectBox query
Paging library documentation
ObjectBox Architecture Components example code
ObjectBox support for LiveData
build queries