gpt4 book ai didi

ios - SetValue 未按预期运行

转载 作者:行者123 更新时间:2023-11-28 12:40:15 27 4
gpt4 key购买 nike

我在使用 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来检测这一点。

这是 StackOverflow 文档中的一个示例:

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)
})

关于ios - SetValue 未按预期运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39582226/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com