Firebase - 如何获取observeEventType = Value 中的键值 [英] Firebase - how to get the key value in observeEventType = Value

查看:13
本文介绍了Firebase - 如何获取observeEventType = Value 中的键值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是Firebase - 构建数据库的正确方法的后续问题

我有以下数据库结构:

"artists" : {
  "-KKMkpA22PeoHtBAPyKm" : {
    "name" : "Skillet"
  }
}

我想查询艺术家参考,看看艺术家是否已经在数据库中,如果艺术家在数据库中,获取艺术家密钥(在上面的例子中它是 -KKMkpA22PeoHtBAPyKm).

And I want to query the artists ref and see if an artist is already in the DB or not and if the artist IS in the DB, get the artist key (in the above example it would be -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"只给了我艺术家"的父键.

but "snapshot.key" only gives me the parent key which is "artists".

我怎样才能得到我需要的钥匙?

How can I get the key I need?

推荐答案

在if条件下,需要获取allKeys才能得到-KKMkpA22PeoHtBAPyKm"...

In if condition, you need to get allKeys to get "-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")
    }

这篇关于Firebase - 如何获取observeEventType = Value 中的键值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆