使用Firebase的Firestore有效(连续)更新聊天消息 [英] Updating chat messages efficiently (continuously) with Firebase's Firestore

查看:70
本文介绍了使用Firebase的Firestore有效(连续)更新聊天消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个使用Firebase的Firestore作为后端的React Native应用程序.现在,每次收到新消息时,我都是从Firestore提取所有消息并更新我的状态,尽管这只是一条新消息.

I'm working on a React Native app that uses Firebase's Firestore for backend. Right now, every time a new message comes, I'm fetching all messages from the Firestore and updating my state, although it's just one new message that came.

function listenCurrentChat(dispatch, chatID) {
    const address = "chats/" + chatID + "/messages";
    firebase.firestore().collection(address).orderBy('createdAt')
        .onSnapshot(function (querySnapshot) {
            dispatch({
                type: CLEAR_MESSAGES,
                payload: {
                    chatID: chatID,
                }
            });
            querySnapshot.forEach(function (doc) {
                //local
                if (doc.metadata.hasPendingWrites) {
                    dispatch({
                        type: ADD_MESSAGE,
                        payload: {
                            chatID: chatID,
                            message: {...doc.data(), createdAt: new Date()}
                        }
                    });
                } else {
                    dispatch({
                        type: ADD_MESSAGE,
                        payload: {
                            chatID: chatID,
                            message: {...doc.data(), createdAt: doc.data().createdAt.toDate()}
                        }
                    });
                }
            });
        });
}

显然,这很糟糕,因为我对一条新消息使用了多次读取,而且由于时滞不断,因此用户体验也根本不好.我还尝试将这些实时侦听器的地址保存为redux状态,并从最后一条消息的时间戳进行侦听,但这似乎也不是一个好的解决方案,因为我不再考虑先前的消息可以编辑.什么是在React Native上更新聊天对话的好方法,以便我尽可能少地使用Firestore读取内容?

Obviously that's very bad, because I'm using many reads for just one new message, and also the user experience is not good at all, as there is constant lag. I also tried saving the addresses of these real time listeners in the redux state, and listening from the last message's timestamp, but that didn't seem like a good solution either, as I was not taking into consideration any more the fact that previous messages could be edited. What would be a good way to update chat conversations on the React Native part, so that I use as less Firestore reads as possible?

谢谢.

推荐答案

我的看法是,您的代码在查询上添加了一个侦听器,每次结果更改时,该侦听器就会被调用.

The way I see it, your code is adding a one listener on a query, which will get invoked with the results of the query every time those results change.

听起来您好像在想这每次都会重新读取每个文档.那不是Firestore侦听器工作的方式.只要连接了该侦听器,它将仅读取对该查询的更改,而不读取查询的整个结果.该查询中任何不变的文档都不会重新读取,即使您观察到它已重新传递给侦听器也是如此.只要连接了侦听器,Firebase SDK就会在内部将所有文档缓存在内存中,并且只会读取与服务器保持同步所需的最少文档数.

It sounds like you are thinking that this re-reads every document every time. That's not the way Firestore listeners work. As long as that listener is attached, it will only read changes to that query, not the entire results of the query. Any document from that query that doesn't change will not get re-read, even though you are observing that it gets re-delivered to the listener. The Firebase SDK internally keeps all the documents cached in memory for as long as that listener is attached, and it will only read the minimal number of documents that are necessary to stay in sync with the server.

换句话说,它已经使用了最少的读取次数.

In other words, it is already using the minimal number of reads.

这篇关于使用Firebase的Firestore有效(连续)更新聊天消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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