SetValue 未按预期运行

SetValue is not behaving as expected

我在使用 Swift.

编写的 iOS 应用程序中使用 Firebase
    static func createUserRecord(uid: String, user: User){
       let userDictionary = user.getJSON()
       self.usersRef.child(uid).setValue(userDictionary)
    }

这段代码在我发布我的应用程序之前运行良好,在发布该应用程序之后我无法再添加用户记录,该应用程序的所有其他功能都运行良好,包括保存和检索一些数据。谁能发现这段代码中的错误?

即使路径不存在,setValue 也能正常工作吗?例如如果路径 a/b/ 存在,我调用了 a/b/c.child('d').setValue("abcd")。这行得通吗?

调用 setValue() 将创建路径(如果路径尚不存在)。

您的 user.getJSON() 似乎更有可能包含根据数据库(或您的安全规则)无效的内容。

您需要 attach a completion block 检测到这一点。

这是 Whosebug 文档中的一个执行此操作的示例:

A frequent reason why your read operation may not work is because your security rules reject the operation, for example because you're not authenticated (by default a database can only be accessed by an authenticated user).

You can see these security rule violations in the Console output. But it's easy to overlook these. You can also handle them in your own code and make them more prominently visible, which is especially useful during development (since your JSON, rules and code change often).

To detect a failed read on iOS you must implement the withCancel block of your observer:

ref!.child("notAllowed").observe(.value, with: { (snapshot) in
    print("Got non-existing value: \(snapshot.key)")
}, withCancel: { (error) in
    print(error)
})