【发布时间】:2021-02-24 19:55:06
【问题描述】:
JSON
switch
uid
switch : true
uid2
switch : false
我尝试过的方法不起作用
@objc func didLongPress() {
let databaseRef = Database.database().reference().child("switch").child(self.postID)
databaseRef.queryOrdered(byChild: "switch").queryEqual(toValue: "true").observeSingleEvent(of: .value) { (snapshot) in
print(snapshot)
if snapshot.exists() {
print("Address is in DB")
} else {
print("Address doesn't exist")
}
}}
//////note it picks up self.postID successfully so error is after that. print snapshot gives null
也试过
let databaseRef = Database.database().reference().child("switch").child(self.postID).child("switch/\true")
databaseRef.observeSingleEvent(of: .value) { (snapshot) in
print(snapshot)
if snapshot.exists() {
print("Address is in DB")
} else {
print("Address doesn't exist")
}
}
}
所以理论上这应该是一项非常简单、基本的任务。上述尝试中的第一个应该有效。没有权限错误,但控制台仅生成 null 作为快照打印。太奇怪了。
如果我测试一个简单的快照以查看使用.child(switch).child(self.postID).child(switch) 打印的内容 - 它打印 1 表示真,0 表示假。这样就行了。
编辑:我认为这是因为它在目标 c 函数中。我才意识到这一点。我在目标 C 中需要它,因为如果在长按时被调用,它使用目标 c 函数
【问题讨论】:
-
这读起来像XY problem。你想达到什么目的? 1) 查找
switch属性值为true的UID 节点? 2) 检查switch是否是特定的已知UID 节点的true? -
后者。单击用户的图片后,它会检查他或她的值是真还是假
-
问题在于您的查询,我正在链接your other question,因为答案最终会是相同的。
标签: ios swift firebase firebase-realtime-database snapshot