快速将评论上传到Firebase数据库

时间:2018-11-23 01:30:28

标签: ios swift firebase firebase-realtime-database

我正在尝试将评论上传到帖子中,以便我的数据库结构看起来像屏幕截图中的那个,我的下载功能可以下载评论,但是我不确定如何创建一个功能以将其上传到适当的帖子。如果我通过postID可以上传评论吗?

func uploadMessage(withContent content: String, withIcon icon: String, withColor color: String, withDate date: String, withuserName userName: String,sendComplete: @escaping (_ status: Bool) -> ()){
        REF_FEEDMESSAGES.childByAutoId().updateChildValues(["content" : content, "color" : color, "icon" : icon, "date" : date, "userName" : userName])
        sendComplete(true)
    }


func getFeedMessages(handler: @escaping (_ feedMessages:[FeedMessages]) -> ()){
        var feedMessagesArray = [FeedMessages]()
        var commentArray = [messageComments]()


        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"
                var color = messages.childSnapshot(forPath: "color").value as? String ?? "bop"
                let date = messages.childSnapshot(forPath: "date").value as? String ?? "0"
                let userName = messages.childSnapshot(forPath: "userName").value as? String ?? "Anonymous"

                let comments = messages.childSnapshot(forPath: "comments").value as? [String: Any] ?? [:]


                for comment in comments {

                    let theComment = comment.value as? [String: Any]

                    let theContent = theComment?["content"] as? String ?? ""
                    let theIcon = theComment?["icon"] as? String ?? ""
                    let theColor = theComment?["color"] as? String ?? ""
                    let theDate = theComment?["date"] as? String ?? ""
                    let theName = theComment?["userName"] as? String ?? ""

                    let aComment = messageComments(content: theContent, color: theColor, icon: theIcon, date: theDate,  userName: theName)
                    commentArray.append(aComment)
                }

                let messages = FeedMessages(content: content, color: color, icon: icon, date: date, comments: commentArray, userName: userName)
                feedMessagesArray.append(messages)
            }

            handler(feedMessagesArray)
        }
    }

database structure

1 个答案:

答案 0 :(得分:1)

这是解决方案,您需要注释的键

func uploadComment(withContent content: String, withIcon icon: String, withColor color: String, withDate date: String, withUserName userName: String, withKey key: String, sendComplete: @escaping (_ status: Bool) -> ()){
        REF_FEEDMESSAGES.child(key).child("comments").childByAutoId().updateChildValues(["content" : content, "color" : color, "icon" : icon, "date" : date, "userName" : userName])
        sendComplete(true)
    }