我正在按照Firebase的指南为聊天应用程序构建数据结构。他们建议采用以下所示的结构。
{
// 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
// converation ID
"messages": {
"one": {
"m1": {
"name": "eclarke",
"message": "The relay seems to be malfunctioning.",
"timestamp": 1459361875337
},
"m2": { ... },
"m3": { ... }
},
"two": { ... },
"three": { ... }
}
}
我该如何组织用户数据,以便能轻松地显示他们参与的所有聊天的列表,并为每个聊天显示最后一条消息和时间戳。如果我采用以下结构:
"users": {
"ghopper": {
"name": "Gary Hopper",
"chats": {
"one: true",
"two": true
}
},
"alovelace" { ... }
},
我可以轻松地获取特定用户(例如ghopper)的每个聊天组列表,方法是在Swift中执行以下操作:
ref.child("users").child("ghopper").child("chats").observeSingleEventOfType(.Value, withBlock: { (snapshot) in
//do something with data
}
但是在这个快照中,我没有最后一条消息和时间戳。我需要做什么才能访问这些数据?
- 为每个用户复制所有这些数据?例如添加users / ghopper / chats / one / {"lastMessage": "ghopper:Relay malfunction found. Cause: moth。", "timestamp":1459361875666}
- 为用户所在的每个聊天创建查询"chats/specificGroupId"(添加多个监听器)?
- 其他办法?