objectbox / objectbox-java

Android Database - first and fast, lightweight on-device vector database
https://objectbox.io
Apache License 2.0
4.41k stars 302 forks source link

Set offset and limit before query build #303

Open sandsaber opened 6 years ago

sandsaber commented 6 years ago

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.

greenrobot commented 6 years ago

Sounds reasonable, keeping this as a feature request.

sapelkinAV commented 6 years ago

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

Queatz commented 6 years ago

Looking for this, too. :+1:

hendrep commented 6 years ago

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?

TinoGuo commented 6 years ago

any update?

ajans commented 3 years ago

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.

Queatz commented 3 years ago

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

ajans commented 3 years ago

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?

greenrobot-team commented 2 years ago

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