从firestore获取最近25条聊天信息(文档)?(Android/Java)

从firestore获取最近25条聊天信息(文档)?(Android/Java),android,firebase,google-cloud-firestore,Android,Firebase,Google Cloud Firestore,我有一个聊天应用程序,我想: -获取最后25条消息 -按升序排序 代码:不工作 firebaseFirestore.collection("chats") .document(chatUid).collection("allchats") .orderBy("chat_datesent",Query.Direction.DESCENDING) .limit(25) .o

我有一个聊天应用程序,我想: -获取最后25条消息 -按升序排序

代码:不工作

firebaseFirestore.collection("chats")
                .document(chatUid).collection("allchats")
                .orderBy("chat_datesent",Query.Direction.DESCENDING)
                .limit(25)
                .orderBy("chat_datesent",Query.Direction.ASCENDING)
                .addSnapshotListener(new EventListener<QuerySnapshot>() {

    public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots,Nullable FirebaseFirestoreException e) {
         if (queryDocumentSnapshots != null) {
             String dated;
             for (DocumentChange doc : queryDocumentSnapshots.getDocumentChanges()) {
                //body
       }
       }
  });
在这里,我尝试使用FirstOrderBy来获取最后25个文档,因为limittolast在中是不可能的 消防商店

我怎样才能做到这一点?我必须使用复合索引吗?
请提供帮助。

不需要两个具有相同字段的Orderby

firebaseFirestore.collection("chats")
                .document(chatUid).collection("allchats")
                .orderBy("chat_datesent",Query.Direction.DESCENDING)
                .limit(25)
                .addSnapshotListener(new EventListener<QuerySnapshot>() {

    public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots,Nullable FirebaseFirestoreException e) {
         if (queryDocumentSnapshots != null) {
             String dated;
             for (DocumentChange doc : queryDocumentSnapshots.getDocumentChanges()) {
                //body
       }
       }
  });

不需要具有相同字段的两个Orderby

firebaseFirestore.collection("chats")
                .document(chatUid).collection("allchats")
                .orderBy("chat_datesent",Query.Direction.DESCENDING)
                .limit(25)
                .addSnapshotListener(new EventListener<QuerySnapshot>() {

    public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots,Nullable FirebaseFirestoreException e) {
         if (queryDocumentSnapshots != null) {
             String dated;
             for (DocumentChange doc : queryDocumentSnapshots.getDocumentChanges()) {
                //body
       }
       }
  });

在收到数据后,您可以使用获取最后25条消息,并对聊天列表进行排序

        firebaseFirestore.collection("chats")
                .document(chatUid).collection("allchats")
                .orderBy("chat_datesent", Query.Direction.DESCENDING)
                .limit(25)
                .addSnapshotListener(new EventListener<QuerySnapshot>() {

                    public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, Nullable FirebaseFirestoreException e) {
                        if (queryDocumentSnapshots != null) {
                            String dated;
                            for (DocumentChange doc : queryDocumentSnapshots.getDocumentChanges()) {
                                //now here you have the list as you wanted but it is not sorted
                                //simply sort the list using something like this

                                //here receive the data in POJO class
                                // assuming you have saved your chat data in chatList

                                Collections.sort(chatList, (item1, item2) -> {
                                    int firstChat = item1.chatDateSent() ? 0 : 1;
                                    int secondChat = item2.chatDateSent() ? 0 : 1;

                                    return first - second; // Ascending
                                }); // using JAVA 1_8
                                //now your list is sorted ASC, notifyDataSetChanged();
                            }
                        }
                    }
                });
如果你需要更多的帮助,请告诉我


希望这会有帮助

在收到数据后,您可以使用获取最后25条消息,并对聊天列表进行排序

        firebaseFirestore.collection("chats")
                .document(chatUid).collection("allchats")
                .orderBy("chat_datesent", Query.Direction.DESCENDING)
                .limit(25)
                .addSnapshotListener(new EventListener<QuerySnapshot>() {

                    public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, Nullable FirebaseFirestoreException e) {
                        if (queryDocumentSnapshots != null) {
                            String dated;
                            for (DocumentChange doc : queryDocumentSnapshots.getDocumentChanges()) {
                                //now here you have the list as you wanted but it is not sorted
                                //simply sort the list using something like this

                                //here receive the data in POJO class
                                // assuming you have saved your chat data in chatList

                                Collections.sort(chatList, (item1, item2) -> {
                                    int firstChat = item1.chatDateSent() ? 0 : 1;
                                    int secondChat = item2.chatDateSent() ? 0 : 1;

                                    return first - second; // Ascending
                                }); // using JAVA 1_8
                                //now your list is sorted ASC, notifyDataSetChanged();
                            }
                        }
                    }
                });
如果你需要更多的帮助,请告诉我


希望这会有帮助

请发布您的数据库结构。为什么使用.orderBy进行两次不同的排序order@RahulGaur我试着检查它是否有效,但没有。我只想查询按升序排序的最后25个文档。你能帮个忙吗?请发布你的数据库结构。你为什么使用.orderBy两次不同的排序order@RahulGaur我试着检查它是否有效,但没有。我只想查询按升序排序的最后25个文档。你能帮忙吗?谢谢你的回答。不过,如果我使用您的代码,它会显示前25个文档。我想获取最后25个文档。请尝试将方向更改为DESCENDING@RahulGaur对但是我怎样才能把它排序回升序呢?想象一个聊天场景。我可以使用collections.sort,但我想通过查询本身对其进行排序。是否有任何按钮可以更改排序顺序?谢谢您的回答。不过,如果我使用您的代码,它会显示前25个文档。我想获取最后25个文档。请尝试将方向更改为DESCENDING@RahulGaur对但是我怎样才能把它排序回升序呢?想象一个聊天场景。我可以使用collections.sort,但我想通过查询本身对其进行排序。是否有任何按钮可以更改排序顺序?非常感谢Rahul。尽管如此,难道不可能从服务器获得正确的排序结果吗?这可能会产生问题,例如:如果用户发送新消息,它将显示在顶部而不是底部。非常感谢Rahul。尽管如此,难道不可能从服务器获得正确的排序结果吗?这可能会产生问题,例如:如果用户发送新消息,它将显示在顶部而不是底部。