我正在关注 Firebase 指南结构化数据 https://firebase.google.com/docs/database/ios/structure-data#flatten_data_structures对于聊天应用程序。他们建议的结构如下所示。
{
// 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)的每个聊天组的列表:
ref.child("users").child("ghopper").child("chats").observeSingleEventOfType(.Value, withBlock: { (snapshot) in
//do something with data
}
但是,我在此快照中不会有最后一条消息和时间戳。我需要做什么才能访问这些数据?
- 为每个用户复制所有这些数据?即添加 users/ghopper/chats/one/ {"lastMessage": "ghopper: 发现继电器故障。原因:飞蛾。", "timestamp" : 1459361875666}
- 为用户所属的每个聊天查询“chats/specificGroupId”(添加多个侦听器)?
- 还有其他方法吗?