Skip to content

Set offset and limit before query build #303

Open

Description

@sandsaber

Currently, we can only use limit and offset as .find(long offset, long limit)

But, what If I want to listen to query changes but with limit and offset as results? Is that possible?
Example, I have 1000 messages, but I do show for a user only 100, if a user will scroll, then new messages will be loaded. Also, query automatically updates list if, a new message will be inserted into DB, but with same offset and limit.

Activity

  1. greenrobot commented on Dec 7, 2017

    @greenrobot
    Member

    Sounds reasonable, keeping this as a feature request.

  2. sapelkinAV commented on Mar 18, 2018

    @sapelkinAV

    I really need that feature too) Is it in work?

  3. Queatz commented on Sep 21, 2018

    @Queatz

    Looking for this, too. 👍

  4. hendrep commented on Oct 5, 2018

    @hendrep

    So if I understand correctly there is currently no way to limit the amount of objects an observable query returns, other than filtering it with properties?
    Say I am only interested in the latest 5 objects, is there any way to get only the latest five objects via a query?

  5. TinoGuo commented on Oct 23, 2018

    @TinoGuo

    any update?

  6. added this to the 2.2.1 milestone on Nov 5, 2018
  7. modified the milestones: 2.3.0, 2.4.0 on Dec 30, 2018
  8. removed this from the 2.4.0 milestone on Oct 3, 2019
  9. added this to the 3.0 milestone on Jul 7, 2020
  10. ajans commented on Feb 22, 2021

    @ajans

    Is it feasible to use the ObjectBox-support for Jetpack Pagination for this?

    Please see my comment on this other closed issue for more details on how to possibly do that.

  11. Queatz commented on Jul 5, 2021

    @Queatz

    I am also looking for a way to limit the number of results returned from an observer.

  12. ajans commented on Jul 8, 2021

    @ajans

    I didn't test this, but it should be possible 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 would have to add the Android Jetpack Paging dependency 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)

    Would that solve your problem?

  13. greenrobot-team commented on Apr 4, 2022

    @greenrobot-team
    Member

    If the above ObjectBoxDataSource is not an option, here is how to get started with your own general DataObserver that runs a query with limit and offset:

    class LimitOffsetNoteObserver(
        private val store: BoxStore,
        private val observer: DataObserver<List<Note>>,
        private val offset: Long,
        private val limit: Long
    ) : DataObserver<Class<Note>> {
        override fun onData(data: Class<Note>) {
            val results = store.boxFor(Note::class)
                .query(Note_.text.contains("TODO"))
                .build()
                .find(offset, limit)
            observer.onData(results)
        }
    }
    
    // Keep the subscription while used 
    // to avoid garbage collection of the observer.
    subscription = store.subscribe(Note::class.java)
        .observer(LimitOffsetNoteObserver(store, observer, offset, limit))
  14. greenrobot-team commented on Sep 21, 2026

    @greenrobot-team
    Member

    Side note: this also affects the RxJava(3) wrapper classes: greenrobot/ObjectBoxRxJava#3

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

Metadata

Metadata

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions