gpt4 book ai didi

ios - Firebase 查询(在 Swift 中)以在应用程序的所有用户中查找特定值

转载 作者:搜寻专家 更新时间:2023-10-31 22:57:43 25 4
gpt4 key购买 nike

我对 Swift 和 Firebase 比较陌生,我正在寻找一种查询 Firebase 数据库以查找特定值的方法。在我正在开发的应用程序的上下文中,当用户注册时,他必须选择一个用户名,我们想在数据库中验证没有其他用户已经拥有所选的用户名。我的数据库结构如下:

Image of DB structure

假设我正在应用程序中注册,我希望我的用户名是“mike”。我希望应用程序在数据库中的所有“用户名”字段中进行搜索,以检查“mike”是否已被占用,但我的 Firebase 代码不起作用:

let queryRef = FIRDatabase.database().reference().child("users")
queryRef.queryOrdered(byChild: "username")
.queryEqual(toValue: self.username.text)
.observeSingleEvent(of: .value, with: { snapshot in
if snapshot.exists()
{
print(snapshot)
}
})

“快照输入”之后的所有内容甚至都不会执行(它会跳过指令集)。现在,在进行实际检查之前,我只是在测试尝试打印“快照”值,但它从未被打印出来。

我这样设置我的数据库规则,因为任何试图查询它的人都需要访问“用户名”键:

{
"rules": {
"users": {
"$uid": {

"email": {
".read": "$uid === auth.uid",
".write": "$uid === auth.uid",
},
"username": {
".read": true,
".write": "$uid === auth.uid",
}

}
}
}
}

对我而言,除了“用户名” key 外,所有内容都保密,这对我来说很重要。在此示例中,为了简单起见,我只有“电子邮件”和“用户名”键,但在我的实际应用程序中,我有更多(名字、姓氏、出生日期、电话号码等)

在这一点上的任何帮助将不胜感激。非常感谢

最佳答案

我认为这是不可能的。

来自 Firebase 文档:

Rules are applied in an atomic manner. That means that a read or write operation is failed immediately if there isn't a rule at that location or at a parent location that grants access. Even if every affected child path is accessible, reading at the parent location will fail completely.

在您的情况下,当您尝试按 username 进行过滤但父节点 users 没有规则时,您将无法完成过滤PERMISSION_DENIED 失败。

您可以访问 users/a_valid_uid/username,因为整个路径都是可访问的。

解决此问题的方法是创建一个仅包含用户名的单独节点,并将其提供给公众阅读。

您可以查看有关 Firebase 规则的完整文档 here .

注意:您可以使用 Firebase 规则模拟器在部署之前测试您的规则。

关于ios - Firebase 查询(在 Swift 中)以在应用程序的所有用户中查找特定值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42799615/

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