Skip to content

Latest commit

 

History

History
54 lines (40 loc) · 2.75 KB

paging-architecture-components.md

File metadata and controls

54 lines (40 loc) · 2.75 KB
description
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.

Paging (Arch. Comp.)

{% hint style="info" %} Since 2.0.0 {% endhint %}

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.

{% hint style="info" %} Note: the following assumes that you have already added and set up the Paging library in your project. {% endhint %}

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:

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.

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

Next steps