Firebase实时数据库帖子

时间:2018-07-20 02:51:43

标签: ios swift firebase

我正在开发一个社交媒体应用,我想将帖子上传到firebase,但是它不会显示在实时数据库中

let DB_BASE =  Database.database().reference()
private var _REF_NOW = DB_BASE.child("now").childByAutoId()

var REF_NOW: DatabaseReference {
    return _REF_NOW
}

func uploadPost(withTitle title: String, forUID uid: String,
                withDescription description: String, sendComplete: @escaping (_ status:
    Bool) -> ()) {
    REF_NOW.childByAutoId().updateChildValues(["Title": title, "Id": uid,
                                               "Description": description])
    sendComplete(true)
}

@IBAction func postPressed(_ sender: Any) {
    if textfield.text != nil && textview.text != nil && textview.text != ""
    {
        postBtn.isEnabled = false
        DataService.instance.uploadPost(withTitle: textfield.text!, forUID:
        (Auth.auth().currentUser?.uid)!, withDescription: textview.text) {
            (isComplete) in
            if isComplete {
                self.postBtn.isEnabled = true
                self.dismiss(animated: true, completion: nil)
            } else {
                self.postBtn.isEnabled = true
                print("There was an error!")
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您的childByAutoId已复制,请尝试使用setValue而不是updateChildValues

private var _REF_NOW = DB_BASE.child("now")

REF_NOW.childByAutoId().setValue(["Title": title, "Id": uid, 
"Description": description])