I have a chat project using Firebase Android SDK for the server. After 3 months, my application has many users and they chat with each other a lot. It takes a long time to load all of any chat list from Firebase. So now, I wonder that how to paging query from Firebase using Android SDK.
I researched this link to find a query command for this problem but fail. I only query a number of the row from first or last. I could not query from random locate in Firebase. Example using skip
and take
key to query.
If you have any document about that. Please tell me. Thanks for advance.
Concepts like skip()
or take()
don't map well to the realtime world of the Firebase Database. That's why pagination will also be challenging to implement. Technically it is possible, but for a good user experience it's important you keep in mind that the data on a page may change while the user sees it.
Back to how you can implement pagination: the startAt()
and endAt()
methods take a optional second parameter, which is documented as (emphasis mine):
public Query startAt (String value, String key)
Create a query constrained to only return child nodes with a value greater than or equal to the given value, using the given
orderBy
directive or priority as default, and additionally only child nodes with a key greater than or equal to the given key.
So if you for example have a list of items sorted by date, you can pass in the date and key of the last item on the previous page to get the next page. You'll have to request one extra item (often called the anchor item), but the overhead should be small.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With