【发布时间】:2016-11-12 00:38:19
【问题描述】:
在 Firebase 结构化您的数据部分,讨论了以下示例,其中聊天消息存储在聊天对话 ID 下。
考虑到这种特殊情况,我们在messages 下聊天one,所有消息都存储在m1、m2 等之下。
现在当用户在主屏幕上时,它应该列出所有对话,他/她是其中的一部分。在任何情况下,都会有新消息到达,UI 将更新以显示新消息。但为此,我们需要将侦听器绑定到每个对话,例如:onChildAdded for one,onChildAdded for two 等等。
如何收听onChildAdded 的任何对话?在messages 上设置onChildUpdate 将发送包含所有对话的DataSnapshot。如果有大量记录,那将是不可行的。查找整个快照的哪个子节点实际上已更新是一件很痛苦的事情。
如果我选择将消息直接存储在messages 下,而每条消息都包含conversationId 字段以检查其所属的位置。通过在conversationId 上创建indexOn,我可以通过为messages 设置值侦听器来关注添加或更新的每个孩子。但是我认为一个非常大的数据库可能存在性能问题。
需要更新任何孩子是因为我需要对消息的统计信息进行一些计算。
有没有可能实现这种情景?无论用户在应用程序中的位置如何,我都需要关注每个对话的同步数据(在 Application 类中设置侦听器可能是一个不错的选择)以在其他地方更新多个值。
我指的数据是这样的:
{
// Chats contains only meta info about each conversation
// stored under the chats's unique ID
"chats": {
"one": {
"title": "Historical Tech Pioneers",
"lastMessage": "ghopper: Relay malfunction found. Cause: moth.",
"timestamp": 1459361875666
},
"two": { ... },
"three": { ... }
},
// Conversation members are easily accessible
// and stored by chat conversation ID
"members": {
// we'll talk about indices like this below
"one": {
"ghopper": true,
"alovelace": true,
"eclarke": true
},
"two": { ... },
"three": { ... }
},
// Messages are separate from data we may want to iterate quickly
// but still easily paginated and queried, and organized by chat
// conversation ID
"messages": {
"one": {
"m1": {
"name": "eclarke",
"message": "The relay seems to be malfunctioning.",
"timestamp": 1459361875337
},
"m2": { ... },
"m3": { ... }
},
"two": { ... },
"three": { ... }
}
}
【问题讨论】:
-
那么您找到解决问题的方法了吗?
-
@user2924714 Nop.. 当用户进入一个聊天时,我尝试检查和更新数据。如果用户退出聊天,则停止侦听该节点上的更改。没有别的了。
标签: android firebase firebase-realtime-database