Max F
Max F

Reputation: 39

Numerical pagination in Firestore

I'm developing an Android game in Kotlin. As a part of it I would like to fetch a leaderboard of players from Firebase. I am trying to paginate it numerically (i.e. page 1: players 1-50, page 2: players 51-100, etc.) I've read that this is not possible, because Firestore doesn't support this type of pagination. Is this true?

Thanks in advance for your help.

Upvotes: 0

Views: 1214

Answers (2)

Acid Coder
Acid Coder

Reputation: 2756

Numeric index pagination is not perfectly possible in Firestore

example if you want to query item from the 10th to 20th, this is not possible unless

you create an index field in the document to keep track the index, then it is possbile

however thing become complicated if your item is deletable, for example if you delete item 12th, you need to reindex every item start from 13th, which make thing soo complicated and expensive

so the trick to make numeric pagination possible, is to forbid item deletion

however you still want to respect the right of the user, so when user deleted his content, replace the content with placeholder "content is deleted"

then you can happily implement numeric index pagination in Firestore

Upvotes: 0

Doug Stevenson
Doug Stevenson

Reputation: 317903

With Firestore, you can request pages of data of a certain size. That's what limit(N) is for. So, you can start at the beginning of a query, and get a page of size N, then continue that query with another page of size N. You generally start at page 1, then progress through the pages using the provided API by specifying which document was the last one in the last page.

What Firestore won't do for you is let you jump immediately to an arbitrary page without first reading all the prior pages. There is no way to tell a query "start at item 100". You can start a specific document, and you can start at a specific value of an ordered field, but that's it. Also, since collection don't maintain a document count, you won't be able to know how many pages of data there are, unless you maintain your own count of documents.

Upvotes: 1

Related Questions