使用Firebase更新值

时间:2019-04-09 11:16:29

标签: swift firebase firebase-realtime-database

我有一个Firebase引用,我想更新Firebase的引用,但是当我尝试使用Could not cast value of type '__NSCFBoolean' (0x2106be078) to 'NSDictionary'

时,我的应用程序就会崩溃

我的裁判在下面看起来像这样

Database.database().reference(forLastMsg: championId).child(toID).updateChildValues(withValues)
                                Database.database().reference(forLastMsg: toID).child(championId).updateChildValues(withValues)

即使使用setValue也会使应用程序崩溃。

实际上,我一直在不断列出已更新的引用,例如valueChange

for snapshot in snapshot.children {
                        let receivedMessage = (snapshot as! DataSnapshot).value as!
                            NSDictionary
...

enter image description here

2 个答案:

答案 0 :(得分:1)

您必须以这种方式更新

let updateDatabase = databaseReference.child("something").child(someId)

    let data = [
        "boolValue": myBoolean,
        "dateUpdated": Date().toSeconds()
        ] as [String : Any]

    updateDatabase.updateChildValues(data)

答案 1 :(得分:0)

要更新

let newVal = //// some dictionary 
 // you may add komal_xyz as dynamic according to your logic
Database.database().reference().child("komal_xyz/\(id)").updateChildValues(newVal) { (err, ref) in

}

Database.database().reference().child("komal_xyz/\(id)").observe(.value) { (ref) in
   //  

   let res = ref.value as! [String:Any]    
}