我正在做一个聊天信使,我能够检索我发送给另一个用户的所有消息,但无法检索他们发送的任何内容。我用来加载邮件的代码是
func loadMsg() {
let toId = user!.id!
let fromId = Auth.auth().currentUser!.uid
let ref = Database.database().reference().child("privateMessages").child(fromId).child(toId)
ref.observe(.value) { (snapshot) in
if let snapshot = snapshot.children.allObjects as? [DataSnapshot] {
self.messages.removeAll()
for data in snapshot {
let newMsg = Message(dictionary: data.value as! [String: AnyObject])
self.messages.append(newMsg)
}
}
DispatchQueue.main.async {self.tableView.reloadData()}
}
}
和
的json文件{
"privateMessages" : {
"0YfqnPIOYFYKb8cYZMHnSYti62i2" : {
"StHPXFvTlVf5QDvWiuCi4JF8Hyr2" : {
"-LB0AZRcWF0Ub5ZECzTf" : {
"fromId" : "0YfqnPIOYFYKb8cYZMHnSYti62i2",
"textMessages" : "Hi!first msg!",
"timestamp" : 1524733200,
"toId" : "StHPXFvTlVf5QDvWiuCi4JF8Hyr2"
}
},
"kj7vcszbSvPBTVaC32Xg18aYZPi1" : {
"-LB0EpR86dZSYl5p0k-E" : {
"fromId" : "0YfqnPIOYFYKb8cYZMHnSYti62i2",
"textMessages" : "Different guy,message 2",
"timestamp" : 1524734318,
"toId" : "kj7vcszbSvPBTVaC32Xg18aYZPi1"
}
}
},
"2wYq9dCKF4aZ26nOY41ApPOdGrJ2" : {
"0YfqnPIOYFYKb8cYZMHnSYti62i2" : {
"-LBV5jlvtxbZZJFQOwes" : {
"fromId" : "2wYq9dCKF4aZ26nOY41ApPOdGrJ2",
"textMessages" : "hi",
"timestamp" : 1525252029,
"toId" : "0YfqnPIOYFYKb8cYZMHnSYti62i2"
}
},
"StHPXFvTlVf5QDvWiuCi4JF8Hyr2" : {
"-LBVCQ5GaEugMNFMT2c-" : {
"fromId" : "2wYq9dCKF4aZ26nOY41ApPOdGrJ2",
"textMessages" : "hi",
"timestamp" : 1525253780,
"toId" : "StHPXFvTlVf5QDvWiuCi4JF8Hyr2"
}
}
},
"StHPXFvTlVf5QDvWiuCi4JF8Hyr2" : {
"0YfqnPIOYFYKb8cYZMHnSYti62i2" : {
"-LBVGEFG76z6tXtq43k5" : {
"fromId" : "StHPXFvTlVf5QDvWiuCi4JF8Hyr2",
"textMessages" : "Hi",
"timestamp" : 1525254780,
"toId" : "0YfqnPIOYFYKb8cYZMHnSYti62i2"
}
}
}
},
"users" : {
"0YfqnPIOYFYKb8cYZMHnSYti62i2" : {
"email" : "test@yahoo.com",
"id" : "0YfqnPIOYFYKb8cYZMHnSYti62i2",
"name" : "tester",
"profileImageUrl" : "https://firebasestorage.googleapis.com/v0/b/groupchatnappointment.appspot.com/o/profile_images%2FE509EFCB-E41D-4E6C-922B-01B146FD1FDC.png?alt=media&token=a7acb904-474b-4898-b99a-1e819ec96afc"
},
"StHPXFvTlVf5QDvWiuCi4JF8Hyr2" : {
"email" : "test2@yahoo.com",
"id" : "StHPXFvTlVf5QDvWiuCi4JF8Hyr2",
"name" : "tester2",
"profileImageUrl" : "https://firebasestorage.googleapis.com/v0/b/groupchatnappointment.appspot.com/o/profile_images%2F2A5009D7-51C4-4D95-88DF-DADB38C76E7B.png?alt=media&token=ae599135-8ff7-4c64-9667-b9a5cec3dcf8"
},
"kj7vcszbSvPBTVaC32Xg18aYZPi1" : {
"email" : "tester3@yahoo.com",
"id" : "kj7vcszbSvPBTVaC32Xg18aYZPi1",
"name" : "tester3",
"profileImageUrl" : "https://firebasestorage.googleapis.com/v0/b/groupchatnappointment.appspot.com/o/profile_images%2FD60F2433-14E2-4EE1-AA74-8171CBA1D3AD.png?alt=media&token=728f6171-e48d-4bae-9b84-007937ed3493"
}
}
}
我的tableviewCells的配置函数为
func configCell(message: Message) {
self.message = message
if message.fromId == currentUser {
sentView.isHidden = false
sentMsgLabel.text = message.textMessages
receivedMsgLabel.text = ""
receivedMsgLabel.isHidden = true
} else {
sentView.isHidden = true
sentMsgLabel.text = ""
receivedMsgLabel.text = message.textMessages
receivedMsgLabel.isHidden = false
}
}
我该如何尝试能够显示收件人发送给我的内容以及我在聊天记录中发送给他们的内容?
答案 0 :(得分:2)
通常在聊天应用中,我将数据建模为"聊天室"。因此,如果两个人之间有聊天,我会为该对话创建一个节点。
这看起来与您的数据结构非常相似,但现在只有一个节点用于"房间"。如果可能,我喜欢将房间ID基于参与者的UID(有关详细信息,请参阅http://stackoverflow.com/questions/33540479/best-way-to-manage-chat-channels-in-firebase)。所以在你的情况下:
privateChats
0YfqnPIOYFYKb8cYZMHnSYti62i2_StHPXFvTlVf5QDvWiuCi4JF8Hyr2
-LB0AZ...CzTf
...
-LB0F1...pvyJ
...
通过这种结构,您可以通过查找房间,然后只是列出消息,轻松显示两个用户之间的整个对话。
对于"对话列表"对于用户,我然后为每个用户建模一个额外的节点:
chatsPerUser
0YfqnPIOYFYKb8cYZMHnSYti62i2
0YfqnPIOYFYKb8cYZMHnSYti62i2_StHPXFvTlVf5QDvWiuCi4JF8Hyr2: ...
StHPXFvTlVf5QDvWiuCi4JF8Hyr2
0YfqnPIOYFYKb8cYZMHnSYti62i2_StHPXFvTlVf5QDvWiuCi4JF8Hyr2: ...
在这里,您可以保留显示会话列表所需的任何信息,例如"房间名称" (仅适用于有> 2个参与者的房间)或最新消息的时间戳(用于按时间顺序显示对话)。
答案 1 :(得分:2)
实际上你的节点错误.child(fromId).child(toId)
这样你只能获取一方消息。
let chatRoomId = (fromId < toId) ? fromId + "_" + toId : toId + "_" + formId
/// It will look like "0YfqnPIOYFYKb8cYZMHnSYti62i2_kj7vcszbSvPBTVaC32Xg18aYZPi1"
然后在此处存储所有消息,并在需要获取时以同样的方式生成chatRoomId
。通过这种方式,您可以轻松地获取这两个用户的所有聊天。我也回答了paging
。因此,请查看Firabase Paging并以块的形式加载邮件。它将带来更好的用户体验。
答案 2 :(得分:0)
您可以尝试将toId
和fromId
保存为字符串吗?
let toId = String(user!.id!)
这对我有用。