按时间戳对X个最新的Firestore文档进行升序排序,并实时添加 [英] Sort X most recent Firestore documents Ascending by timestamp, and add in real time

查看:43
本文介绍了按时间戳对X个最新的Firestore文档进行升序排序,并实时添加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Firestore创建聊天应用程序.我的消息类具有一个 Date 对象形式的时间戳.我希望聊天活动加载最新的X消息,并使用 SnapshotListener 实时将任何新消息添加到底部.到目前为止,这是我到目前为止的内容,它按时间戳对X个最旧的消息进行排序,但是如果已删除完整的X条消息,则以后将忽略所有消息,并且在添加新消息时不显示任何新消息.

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 messages, and add any new ones to the bottom in realtime using a SnapshotListener. Below is what I have so far, which sorts the X oldest messages by timestamp, but ignores all messages after and does not display any new messages when they are added if a full X messages are already pulled down.

 final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(linearLayoutManager);


        Query query = mCollection.orderBy("timestamp",Query.Direction.ASCENDING).limit(mCurrentPage * TOTAL_ITEMS_TO_LOAD);


        query.addSnapshotListener(new EventListener<QuerySnapshot>() {
            @Override
            public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {
                for (DocumentChange documentChange : queryDocumentSnapshots.getDocumentChanges()) {
                    switch (documentChange.getType()) {
                        case ADDED:
                            Message message = documentChange.getDocument().toObject(Message.class);



                            mMessages.add(message);
                            mAdapter.notifyDataSetChanged();

                    }
                }
            }
        });

我已经删除了多余的代码.按降序排序会导致在查询时这样排列邮件,将查询的最新消息显示在顶部,这是行不通的,因为我希望最新的消息在底部向上递减:

I've removed superfluous code. Sorting by descending causes messages to be laid out like this when queried, with newest messages queried appearing at the top, which doesn't work, because I want newest messages at the bottom descending upward:

  • queriedmessage5<-最新查询
  • queriedmessage4
  • queriedmessage3
  • queriedmessage2
  • queriedmessage1<-最早查询的
  • newmessage1<-在查询后添加

通过布局管理器使用setReverseLayout反转RecyclerView可以更正查询消息的顺序,但随后在RecyclerView的顶部插入新消息:

Reversing the RecyclerView using setReverseLayout via the layout manager corrects the order of the queried messages, but then inserts new messages at the top of the RecyclerView :

  • newmessage1<-在查询后添加
  • queriedmessage1<-最早查询的
  • queriedmessage2
  • queriedmessage3
  • queriedmessage4
  • queriedmessage5<-最新查询

推荐答案

您需要对降序排列的消息进行排序,以便能够获取最新的10条消息.

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)

这将为您提供最新的物品.添加消息时,您将获得最新消息,并且会为查询之外的消息获得 REMOVED 事件.

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.

这篇关于按时间戳对X个最新的Firestore文档进行升序排序,并实时添加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆