如何通过键值从Firebase中检索对象 [英] How to retrieve objects from firebase by key value

查看:147
本文介绍了如何通过键值从Firebase中检索对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是firebase的新手,并且拥有我的firebase项目的结构



我想获得所有的对象,即感兴趣的值等于men我写了这样的代码,得到所有按照interes排序的对象:

  let thisUserRef = URL_BASE.childByAppendingPath(profile)

thisUserRef.queryOrderedByChild(Interest)
.observeEventType(.Value,withBlock:{snapshot in

如果让UserInterest = snapshot.value![Interest] as?String {
print(snapshot.key)
}
}

但是我收到nil。

解决方案

您需要遍历所有键值配置文件

 如果让allProfiles = snapshot.v alue as?[String:AnyObject] {
for(_,profile)in allProfiles {
print(profile);
let userInterest = profile [Interest]
}
}

这里_是格式为 KYXA-随机字符串的关键字,配置文件将是该键的元素。



编辑

根据 docs
$ b 尝试 thisUserRef。 queryOrderedByChild(Interest)。equalTo(men)然后使用我在回答中指定的内部循环

I'm new to firebase and I have such structure of my firebase project

I want to get all objects, that "Interested" value is equal to "men"

I wrote such code, to get all object sorted by interes value:

let thisUserRef = URL_BASE.childByAppendingPath("profile")

thisUserRef.queryOrderedByChild("Interest")
     .observeEventType(.Value, withBlock: { snapshot in

     if let UserInterest = snapshot.value!["Interest"] as? String {    
          print (snapshot.key)  
     }
}

But I receive nil.

解决方案

you need to loop through all the key-value profiles

      if let allProfiles = snapshot.value as? [String:AnyObject] {
            for (_,profile) in allProfiles {
                  print(profile);
                  let userInterest = profile["Interest"]
           }
       }

Here _ is the key that is in the format KYXA-random string and profile will be the element for that key.

Edit:

There is querying for child values as per the docs.

Try thisUserRef.queryOrderedByChild("Interest").equalTo("men") and then using the inner loop that i specified in the answer

这篇关于如何通过键值从Firebase中检索对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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