6

I am trying to create a chat using the Firebase-UI library for Cloud Firestore. This github repository contains the relevant code which I am using. The problem comes with the order of the query.

See that the query is specified as:

Query sChatQuery = sChatCollection.orderBy("timestamp").limit(50);

However, I am getting the oldest 50 messages, instead of the newest ones, in the correct order (from old to new). On the other hand, I can use the following query:

Query sChatQuery = sChatCollection.orderBy("timestamp", Query.Direction.DESCENDING).limit(50);

and this retrieves the 50 newest messages, but in the wrong order (newest on top, oldest on bottom). Hence I do not know how to get this right.

I could locally reverse again the result from the query, but I cannot figure out how to do that (I have already gone through the FirebaseUI library with no luck).

EDIT

I have my chat subcollection for each event in my events collection:

events/event_doc/chat/chat_doc

enter image description here

And I would like to get the messages as follows:

Message 1
Message 2
Message 3
Message 4
...

With the default order I get the messages like shown above, but the limit instructions cuts the snapshots from 1 to 50, and the Message 51 (the newest) is not being retrieved.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
b-fg
  • 3,959
  • 2
  • 28
  • 44
  • Have you tried to use `Query sChatQuery = sChatCollection.orderBy("timestamp", Query.Direction.ASCENDING).limit(50);` – Alex Mamo Apr 26 '18 at 10:40
  • Yes, and it seems this is the default order: `Query sChatQuery = sChatCollection.orderBy("timestamp").limit(50);` – b-fg Apr 26 '18 at 10:49
  • Please add your database structure as a scrennshot and indicate the exact order that you want to get, with a concrete example. – Alex Mamo Apr 26 '18 at 11:01
  • Updated the question. – b-fg Apr 26 '18 at 11:13

1 Answers1

2

To solve this, you can use the following query:

Query sChatQuery = sChatCollection
    .orderBy("timestamp", Query.Direction.ASCENDING)
    .whereGreaterThanOrEqualTo("timestamp', desiredTime)
    .limit(50);

If you are using a RecyclerView to display data, the simplest way would be to use the following code:

LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
layoutManager.setReverseLayout(true);
layoutManager.setStackFromEnd(true);
recyclerView.setLayoutManager(layoutManager);

This approch will reverse you the order as you want.

Another approach would be to create your own adapter that extends FirebaseListAdapter and override getItem() method like this:

@Override
public HashMap getItem(int pos) {
    return super.getItem(getCount() - 1 - pos);
}

Another approach would be to get the data from the database, add it to a Collection, a List would be a good solution and then use Collections.reverse(yourList);.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Before posting the question I was already using the `setReverseLayout` trick, but it does not seem like the best solution. About the last approach you mention, How can I do that with the `FirestoreRecyclerAdapter`? – b-fg Apr 26 '18 at 14:08