objectbox-java icon indicating copy to clipboard operation
objectbox-java copied to clipboard

Set offset and limit before query build

Open sandsaber opened this issue 7 years ago • 9 comments

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.

sandsaber avatar Dec 07 '17 10:12 sandsaber

Sounds reasonable, keeping this as a feature request.

greenrobot avatar Dec 07 '17 10:12 greenrobot

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

sapelkinAV avatar Mar 18 '18 13:03 sapelkinAV

Looking for this, too. :+1:

Queatz avatar Sep 21 '18 22:09 Queatz

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?

hendrep avatar Oct 05 '18 05:10 hendrep

any update?

TinoGuo avatar Oct 23 '18 08:10 TinoGuo

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.

ajans avatar Feb 22 '21 12:02 ajans

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

Queatz avatar Jul 05 '21 17:07 Queatz

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?

ajans avatar Jul 08 '21 14:07 ajans

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))

greenrobot-team avatar Apr 04 '22 09:04 greenrobot-team