Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StorIO and pagination #799

Open
Rainer-Lang opened this issue Jun 16, 2017 · 3 comments
Open

StorIO and pagination #799

Rainer-Lang opened this issue Jun 16, 2017 · 3 comments
Assignees
Labels

Comments

@Rainer-Lang
Copy link

Has anyone an example how to do pagination with StorIO?

@nikitin-da
Copy link
Collaborator

Hello, @Rainer-Lang !
You can apply offset and limit in query to specify rows you are interested in:

Query.builder()
        .table("tweets")
        .orderBy("date")
        .limit(30, 60) // offset and limit
        .build();

If you have some increasing primary key you can insure consistency if beginning of the list will change while pagination:

Query.builder()
        .table("tweets")
        .where("id < ?")
        .whereArgs(previousId)
        .orderBy("id DESC")
        .limit(30)
        .build();

Or you can just use get().cursor and cursor adapter=)

@nikitin-da nikitin-da self-assigned this Jun 16, 2017
@Rainer-Lang
Copy link
Author

@nikitin-da Thanks.
And how about being notified about changes in the tables? Could this be a problem?
I have a service which writes data in the background - and right now my view-data is always up-to-date.

@nikitin-da
Copy link
Collaborator

nikitin-da commented Jun 19, 2017

Changes doesn't contain which particular raws were affected, because we can't determinate it for example for raw queries.
So I guess there is no pretty way to do this =(

This is one of possible solutions: observe table and query loaded pages using shared field - minLoadedId.

private Observable<Tweet> observeTweets() {
    return storio.observeChangesInTable("tables")
            .switchMap(changes -> storio.get().listOfObjects(Tweet.class)
                    .withQuery(
                            Query.builder()
                                    .table("tweets")
                                    .where("id > ?")
                                    .whereArgs(minLoadedId)
                                    .orderBy("id DESC")
                                    .build()
                    )
                    .prepare()
                    .asRxObservable());
}

private Single<Tweets> loadNextPage() {
    return storio.get()
            .listOfObjects(Tweet.class)
            .withQuery(
                    Query.builder()
                            .table("tweets")
                            .where("id > ?")
                            .whereArgs(minLoadedId)
                            .orderBy("id DESC")
                            .limit(30)
                            .build()
            )
            .prepare()
            .asRxSingle()
            .observeOn(AndroidScheduers.mainThread())
            .doOnSuccess(tweets -> minLoadedId = tweets.get(0).id());
}

So it allow fast initial load, but if user will load many pages - updates will be slow and all items will be in memory =(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants