ios - Firebase - 如何获取 observeEventType = Value 中的键值

标签 ios swift firebase firebase-realtime-database

这是对 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/

相关文章:

ios - NSUserDefaults 不适用于 Swift 中的 UITableView

ios - 如果将方法放入 viewDidLoad() 中,为什么应用程序会崩溃?

ios - 新用户与旧 Firebase Facebook 身份验证

javascript - 如何托管 Firebase 函数和单页应用程序?

ios - 是否有像 iOS 8 photo.app 这样的图像裁剪和旋转 View Controller ?

ios - Swift - 此服务器的证书无效

ios - 将 UITapGestureRecognizer 添加到 UIControl

ios - 带有无效签名的无效二进制文件

ios - 单元格滑动事件和按钮触摸事件内

firebase - 使用 Firebase Functions 处理来自 Woocommerce 的多个 Webhook 时,我应该使用单个云函数还是多个云函数? (Webhook 最佳实践)