Skip to content Skip to sidebar Skip to footer

Sort X Most Recent Firestore Documents Ascending By Timestamp, And Add In Real Time

I am attempting to create a chat app using firestore. My message class has a timestamp in the form of a Date object. I would like the chat activity to load the most recent X messag

Solution 1:

You'll need to order the messages descending to be able to get the 10 most recent messages.

mCollection.orderBy("timestamp",Query.Direction.DESCENDING).limit(mCurrentPage * TOTAL_ITEMS_TO_LOAD)

This will give you the most recent items. As you add messages, you'll get the newest ones, and you'll get a REMOVED event for the messages that fall out of the query.

Given that you request the messages in reverse chronological order, you'll have to reverse the message client side again, to show them in the correct order in your app.

Post a Comment for "Sort X Most Recent Firestore Documents Ascending By Timestamp, And Add In Real Time"