这是对 Firebase - proper way to structure the DB 的后续问题
我有以下数据库结构:
"artists" : {
"-KKMkpA22PeoHtBAPyKm" : {
"name" : "Skillet"
}
}
我想查询艺术家引用,看看艺术家是否已经在数据库中,如果艺术家在数据库中,则获取艺术家 key (在上面的示例中,它将是 -KKMkpA22PeoHtBAPyKm).
我试过这个:
artistsRef.queryOrderedByChild("name").queryEqualToValue("Skillet").observeEventType(.Value, withBlock: { (snapshot) in
if snapshot.exists() {
print("we have that artist, the id is \(snapshot.key)")
} else {
print("we don't have that, add it to the DB now")
}
})
但是“snapshot.key”只给了我父键“artists”。
如何获得我需要的 key ?
最佳答案
在 if 条件下,需要获取 allKeys 才能得到 "-KKMkpA22PeoHtBAPyKm"...
if snapshot.exists() {
for a in (snapshot.value?.allKeys)!{
print(a)
}
} else {
print("we don't have that, add it to the DB now")
}
关于ios - Firebase - 如何获取 observeEventType = Value 中的键值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37853533/