【发布时间】:2016-09-27 15:16:33
【问题描述】:
我正在关注 structuring data 的 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)的每个聊天组的列表:
ref.child("users").child("ghopper").child("chats").observeSingleEventOfType(.Value, withBlock: { (snapshot) in
//do something with data
}
但是,我不会在此快照中包含 lastMessage 和时间戳。我需要做什么才能访问这些数据?
- 为每个用户复制所有这些数据?即添加 users/ghopper/chats/one/ {"lastMessage": "ghopper: Relay failure found. Cause: moth.", "timestamp" : 1459361875666}
- 为用户参与的每个聊天查询“chats/specificGroupId”(添加多个侦听器)?
- 其他方式?
【问题讨论】:
标签: swift firebase firebase-realtime-database