【发布时间】:2019-09-04 08:02:57
【问题描述】:
我的数据库如下所示
users
|
@--uidABC
|
|---phone:"+13125550690"
|
|---followers
|
|----uid123
| |---timeStamp:111222
|
|----uid456
| |---timeStamp:777999
我使用.observeSingleEvent 检查uidABC 是否存在,如果存在,我想检查该路径下是否有一个名为followers 的child。
我可以使用snapshot.hasChild("followers") 来查看它是否可用以及是否可用如何循环遍历下面的所有子节点 snapshot.hasChild("...")?
我使用下面的代码,但它循环了错误的snapshot。当它应该使用DataSnapshot 在followers 下的任何内容时,它会使用顶级一级
let ref = Database...child("users").child(uidABC)
ref.observeSingleEvent(of: .value, with: { (snapshot) in
if !snapshot.exists() {
// this user doesn't exist
return
}
if !snapshot.hasChild("followers") {
// this user exists but has no followers
return
}
// this user exists and has followers now loop through them
for uid in snapshot.children {
let snapshot = uid as! DataSnapshot
if let dict = snapshot.value as? [String:Any] {
let timeStamp = dict["timeStamp"] as? Double
// eg. check for a specific timeStamp
}
}
})
【问题讨论】:
标签: ios swift loops firebase-realtime-database