Skip to content

Have no ability to get limited items list through LiveData #475

Closed

Description

@georgiscoolman

I can use find() to get limited items count from query
public List<T> find(final long offset, final long limit)

But unfortunately there is no ability to do the same with LiveData, as it creates based on Query
public ObjectBoxLiveData(Query<T> query)

And in case of huge data it can be performance problem.

Activity

  1. greenrobot-team commented on Jun 18, 2018

    @greenrobot-team
    Member

    Adding a Query build step is the likely way to go, closing in favor of #303. -ut

  2. ajans commented on Feb 19, 2021

    @ajans

    I have the same problem.

    Isn't it possible and feasible to use Android Jetpack Paging with the ObjectBoxDataSource.Factory to get a LiveData-instance that supports limited items query, as described here?

    https://docs.objectbox.io/android/paging-architecture-components

    So, you could do something like this (but you have to integrate Android Jetpack Paging for this to work):

    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;
        }
    }
    

    (copied from above-mentioned objectbox-docs-page)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions