我正在使用Firebase并尝试构建1:1对话。对于房间,我使用了这样的结构:
- rooms
- "user1_user2" // roomName
- timestampedId1
- message: "hello"
- sender: "user2"
- timestampedId2
- message: "hey"
- sender: "user1"
- "user2_user3"
- timestampedId3
- message: "Mew"
- sender: "user3"
对于房间名称,我使用过(我从answer得到的):
let roomName = (from<to ? from+"_"+to : to+"_"+from)
然而,现在我正在尝试检索,我感到困惑。使用Firebase,创建私人房间和检索私人房间的正确结构是什么?
我应该存储&#39;来自&#39;和&#39;到&#39;单独在&#39; roomName&#39;节点?但如果是这样,那么我如何比较它们并将它们列为降序时间戳(新旧)?我认为应该有一种方法可以用一个请求来做。但是我怎么能用这个&#39; roomName&#39;进场?或者还有其他更好的方法来实现它吗?
let roomRef = Firebase(url: self.url + "/rooms")
// some query?
.observeEventOfType(.Value, withBlock: { details in
})
处理此类案件的正确方法是什么?我应该完全改变结构还是有办法正确地查询它?
答案 0 :(得分:2)
我会按如下方式构建它,并有三个节点:roommembers,chat和lastmessages:
roommembers
user1iduser2id
user1id: true
user2id: true
chat
user1iduser2id
-KIyPwdDfAA6GxMlwLJB
message
userid
etc
(-KIyPwdDfAA6GxMlwLJB is a childByAutoId() which is also your timestamp)
lastmessages
user1id
user1iduser2id
lastmessage
lastuser
etc.
user2id
user1iduser2id
lastmessage
lastuser
etc.