【发布时间】:2019-05-23 07:54:29
【问题描述】:
我正在使用 Firestore 实现一个一对一的聊天应用程序,其中有一个名为 chat 的集合,这样集合的每个文档都是不同的 thread。
当用户打开应用程序时,屏幕应显示该用户的所有线程/对话,包括那些有新消息的线程/对话(就像在 whatsapp 中一样)。显然,一种方法是从chat 集合中获取与该用户关联的所有文档。
但是这似乎是一个非常昂贵的操作,因为用户可能只有很少的更新线程(带有新消息的线程),但我必须获取所有线程。
是否有一种优化且成本更低的方法来执行相同操作,其中仅获取具有新消息的线程或更准确地说是用户设备缓存中不存在的线程(新创建或修改的线程)。
chat 集合中的每个文档都有以下字段:
senderID: (id of the user who have initiated the thread/conversation)
receiverID: (id of the other user in the conversation)
messages: [],
lastMsgTime: (timestamp of last message in this thread)
目前要加载某个用户的所有线程,我正在应用以下查询:
const userID = firebase.auth().currentUser.uid
firebase.firestore().collection('chat').where('senderId', '==', userID)
firebase.firestore().collection('chat').where('receiverId', '==', userID)
最后我将这两个查询返回的文档合并到一个数组中以呈现在一个平面列表中。
【问题讨论】:
标签: firebase react-native google-cloud-firestore