How To Paginate In Firebase Database #askfirebase
Solution 1:
You seem to come from a SQL way of thinking, where you paginate by specifying the number of items to get and the number of items to skip. This is index-based pagination.
Firebase on the other uses cursor-based pagination. You tell it the nimber of items to get and at which item to start (or end). You identify this item by the value of the property on which you order, in your case that is the value of word
. Since the same value could potentially appear in multiple children, you can also specify the key (the thing starting with 5v1a1...
) of the child at which to start/end as a second parameter.
So say that you have a page size of two. You get the first 2 words with:
DatabaseReferenceallWords= db.getReference().child("users").child(uid).child("words");
QueryfirstPage= allWords.orderByChild("word").limitToFirst(2);
When you attach a listener to this, you'll get the first two words. You'll need to remember the word
and the key of the last word in this first page:
StringlastWordOnPreviousPage="house";
StringlastKeyOnPreviousPage="5v1a1...";
Now if you need the second page of two words, you get them by:
QuerysecondPage= allWords.orderByChild("word").startAt(lastWordOnPreviousPage, lastKeyOnPreviousPage).limitToFirst(2);
Post a Comment for "How To Paginate In Firebase Database #askfirebase"