首页 > 解决方案 > 使用 firebase 更新值

问题描述

我有一个 firebase ref,我想更新 firebase 的 ref,但是当我尝试它时,我的应用程序崩溃了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
...

在此处输入图像描述

标签: swiftfirebasefirebase-realtime-database

解决方案


你必须以这种方式更新它

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

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

    updateDatabase.updateChildValues(data)

推荐阅读