Firebase queryEqualToValue在Swift 2.3中获得密钥 [英] Firebase queryEqualToValue get Key in Swift 2.3

查看:110
本文介绍了Firebase queryEqualToValue在Swift 2.3中获得密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Swift和Firebase在iOS上开发一个小应用程序。我无法查询数据并检索值。
我的firebase实时数据库是这样的:

 qrcode:{
postsedation_room:http ://bing.com,
preprocedure_room:http://google.com,
presedation_room:http://en.m.wikipedia.org
}

我想这个查询:

  let query = rootRef.child(qrcode).queryEqualToValue(http://bing.com)

query.observeSingleEventOfType(。值,withBlock:{快照在
打印(快照)
})

打印一个关键值postsedation_room。
此时打印的结果是
Snap(qrcode)< null> 能否帮助我,值?

解决方案

当您对Firebase数据库执行查询时,可能会有多个结果。所以如果你附加一个值观察者,快照将包含这些结果的列表。即使只有一个结果,快照将包含一个结果列表。



所以在你的观察者中,你需要处理这个列表。最简单的方法是迭代它:

  let query = rootRef.child(qrcode)。queryOrderedByValue ).queryEqualToValue(http://bing.com)

query.observeSingleEventOfType(.Value,withBlock:{snapshot in
for snapshot.children(
print (child.key)
}
})

另外,您可以观察单 childAdded eventL

  let query = rootRef.child(qrcode ).queryEqualToValue(http://bing.com)

query.observeSingleEventOfType(.ChildAdded,withBlock:{snapshot in
print(snapshot.key)
})


I am developing a small application in iOS using Swift and Firebase. I am having trouble querying a data and retrieving the value. My firebase realtime database is this:

"qrcode" : {
   "postsedation_room" : "http://bing.com",
   "preprocedure_room" : "http://google.com",
   "presedation_room" : "http://en.m.wikipedia.org"
}

I would like to make this query :

let query = rootRef.child("qrcode").queryEqualToValue("http://bing.com")

query.observeSingleEventOfType(.Value, withBlock: { snapshot in
   print(snapshot)
})

print a key value "postsedation_room". At this point the result of print is Snap (qrcode) <null> could you help me so that I can print key given the child value?

解决方案

When you execute a query against the Firebase Database, there will potentially be multiple results. So if you attach a value observer the snapshot contains a list of those results. Even if there is only a single result, the snapshot will contain a list of one result.

So in your observer you'll need to handle that list. The easiest way to do that is to iterate over it:

let query = rootRef.child("qrcode").queryOrderedByValue().queryEqualToValue("http://bing.com")

query.observeSingleEventOfType(.Value, withBlock: { snapshot in
   for child in snapshot.children {
      print(child.key)
   }
})

Alternatively you can observe a single childAdded eventL

let query = rootRef.child("qrcode").queryEqualToValue("http://bing.com")

query.observeSingleEventOfType(.ChildAdded, withBlock: { snapshot in
   print(snapshot.key)
})

这篇关于Firebase queryEqualToValue在Swift 2.3中获得密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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