【发布时间】:2017-11-27 13:18:02
【问题描述】:
我有这个数据库结构:
如果 objectId 值等于特定值,我正在尝试获取节点值(例如:-KzjZJvEQRBlRG8m2RxO)。
我举个例子: 如果对象 id 等于“-Kzx6b-w3cbE_3e1MwWr”,那么我想获取节点值(-Kzx6bVxJHq0HgDjkhH8),以便删除记录。
我试着这样做:
let query = Api.Notification.NOTIFICATION_REF.queryOrdered(byChild: "objectId").queryEqual(toValue: id)
query.observeSingleEvent(of: .value, with: { (snapshot) in
for snap in snapshot.children {
print (snap.key)
}
但是我在 print 语句中没有得到任何值实际上似乎根本没有进入 for 循环。
有没有办法做到这一点?
谢谢!
-----更新 如果我得到很多记录,这样的解决方案会影响速度吗?
Api.Notification.NOTIFICATION_REF.observeSingleEvent(of: .value, with: { (snapshot) in
for child in snapshot.children {
let snap = child as! DataSnapshot
let key = snap.key
Api.Notification.NOTIFICATION_REF.child(key).observeSingleEvent(of: .value, with: { (userid) in
for subChild in userid.children {
let subSnap = subChild as! DataSnapshot
let subKey = subSnap.key
//get the value here
}
})
}
})
【问题讨论】:
-
objectId 值与节点名不同有什么原因吗?
-
是的,它们是不同的,因为根据类型我需要不同的节点名称...例如,如果类型跟随...,我需要一个等于 followeuserId-followinguserId 的值...等等。 ..
-
NOTIFICATION_REF指向什么? -
@FrankvanPuffelen 到主节点:Database.database().reference().child("Notification")
-
这意味着您尝试查询的路径包含两个动态键。 Firebase 数据库只能在您运行查询的每个子节点下的固定路径处查询属性。也可以在这里查看我的答案:stackoverflow.com/questions/27207059/…
标签: swift firebase firebase-realtime-database