1

I am using Firebase Firetore in my Android chat app, and i am ordering messages by timestamp which is added as following when a new message is sent:

Map<String, Object> messageMap = new HashMap<>();
...
 messageMap.put("timestamp", FieldValue.serverTimestamp());

Here is my code to display all the messages in the chat.

Query firstQuery = FirSingleton.getInstance().mMessages.document(channelFoudId).collection("all_messages")
                .orderBy("timestamp", Query.Direction.DESCENDING).limit(limit);


        registration = firstQuery.addSnapshotListener(ChatActivity.this, new EventListener<QuerySnapshot>() {
            @Override
            public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {

                if (e != null) {


                    Log.i(TAG, "onEvent error: " + e.getMessage());
                    progressbar.setVisibility(View.GONE);
                    isLoading = false;
                } else {


                    if (firstTimeLoading) {

                        lastVisible = documentSnapshots.getDocuments().get(documentSnapshots.size() - 1);

                    }

                    for (DocumentChange documentChange : documentSnapshots.getDocumentChanges()) {

                        if (documentChange.getType() == DocumentChange.Type.ADDED) {

                            Log.i(TAG, "onEvent: new added");
                            if (documentChange.getDocument().exists()) {


                                MessageObject messageObject = documentChange.getDocument().toObject(MessageObject.class);

                                if (firstTimeLoading) {
                                    //arrayList.add(messageObject);
                                    arrayList.add(0, messageObject);
                                } else {

                                    arrayList.add(messageObject);
                                    adapter.notifyDataSetChanged();

                                }

                            }
                        }else {

                            Log.i(TAG, "onEvent: no recently added");
                        }
                    }


                    if (firstTimeLoading){
                        recyclerView.getAdapter().notifyDataSetChanged();
                        progressbar.setVisibility(View.GONE);
                        firstTimeLoading = false;
                        isLoading = false;

                    }

                }
            }
        });

The problem is that when the user enters chat activity and there are new messages, the messages are displayed in the opposite order but when refreshing the data (enter the chat activity back) all the messages are in the right order. My question is how to fix this behaviour.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Ali Akkawi
  • 313
  • 4
  • 13
  • I see you are using a `firstQuery`. Are you also using a `secondQuery`? If yes, please also add the code for that. – Alex Mamo Sep 05 '18 at 10:07
  • The second query is for pagination, when we scroll up the RecyclerView, but i guess it has nothing to do with the issue as i currently not listening for other queries until i solve this problem – Ali Akkawi Sep 05 '18 at 10:09
  • I guess it is a timestamp issue, because when i refresh the the activity, all the messages are ordered in the right way – Ali Akkawi Sep 05 '18 at 10:12
  • **[This](https://stackoverflow.com/questions/50741958/how-to-paginate-firestore-with-android)** is a recommended way in which you can paginate queries by combining query cursors with the limit() method. I also recommend you take a look at this **[video](https://www.youtube.com/watch?v=KdgKvLll07s)** for a better understanding. – Alex Mamo Sep 05 '18 at 10:13
  • Thanks, but my problem is not about pagination – Ali Akkawi Sep 05 '18 at 14:22
  • @AlexMamo your provided link is not helpful in chat app. Please don't put your old link in all places – Pooja May 13 '21 at 08:43
  • @Pooja That answer and video, both work perfectly fine ;) – Alex Mamo May 13 '21 at 09:18
  • Yes its working but not for chat app where we need reverse pagination. If its helpful than please give me details. – Pooja May 13 '21 at 09:36

0 Answers0