我正在尝试通过firebase数据库对feedMessages进行注释,但是不知道如何在代码中实现上载/下载过程。如果我希望数据库看起来像显示的图像,该如何在邮件中上传/下载这些评论?
func getFeedMessages(handler: @escaping (_ feedMessages:[FeedMessages]) -> ()){
var feedMessagesArray = [FeedMessages]()
REF_FEEDMESSAGES.observeSingleEvent(of: .value) { (feedMessagesSnapshot) in
guard let feedMessagesSnapshot = feedMessagesSnapshot.children.allObjects as? [DataSnapshot] else {return}
for messages in feedMessagesSnapshot {
let content = messages.childSnapshot(forPath: "content").value as? String ?? "Joe Flacco is an elite QB"
let icon = messages.childSnapshot(forPath: "icon").value as? String ?? "none"
let color = messages.childSnapshot(forPath: "color").value as? String ?? "bop"
let messages = FeedMessages(content: content, color: color, icon: icon, votes: votes)
feedMessagesArray.append(messages)
}
handler(feedMessagesArray)
}
}
func uploadMessage(withContent content: String, withIcon icon: String, withColor color: String,sendComplete: @escaping (_ status: Bool) -> ()){
REF_FEEDMESSAGES.childByAutoId().updateChildValues(["content" : content, "color" : color, "icon" : icon)
sendComplete(true)
}
答案 0 :(得分:0)
您超级亲密!评论节点实际上只是每个消息节点中的子快照,因此您可以将其视为此类。
这是您的代码,带有额外的代码,可打印每个消息节点中的注释。
您需要添加错误检查和代码,以将其添加到FeedMessages。
feedMessagesRef.observeSingleEvent(of: .value) { feedMessagesSnapshot in
guard let feedMessagesSnapshot = feedMessagesSnapshot.children.allObjects as? [DataSnapshot] else {return}
for messages in feedMessagesSnapshot {
let content = messages.childSnapshot(forPath: "content").value as? String ?? "Joe Flacco is an elite QB"
let icon = messages.childSnapshot(forPath: "icon").value as? String ?? "none"
let color = messages.childSnapshot(forPath: "color").value as? String ?? "bop"
print(content, color, icon)
let comments = messages.childSnapshot(forPath: "comments")
for comment in comments.children {
let commentSnap = comment as! DataSnapshot
let aComment = commentSnap.value as! String
print(aComment)
}
let messages = FeedMessages(content: content, color: color, icon: icon, votes: votes)
feedMessagesArray.append(messages)
}
}
编写节点是相同的思考过程-注释节点只是消息中的另一个键:值对,该值是一组键:值对。
在此示例中,我们使用字典来构建结构,然后编写节点
let feedMessagesRef = self.ref.child("feedMessages")
let messageDict:[String: Any] = [
"content": "some content",
"color": "some color",
"icon": "an icon",
"comments":
["comment_0": "a comment",
"comment_1": "another comment"]
]
let aFeedMessage = feedMessagesRef.childByAutoId()
aFeedMessage.updateChildValues(messageDict)