[英]How to make Firebase Database Query to get some of the children using Swift 3?
如何在Firebase数据库中进行查询以在控制台中获取一些子级? 例如,从下面的快照中,如何进行查询以仅获取Image
,其中Des: 11
。
我正在使用此代码:
func loadData(){
Ref=FIRDatabase.database().reference().child("Posts")
Handle = Ref?.queryOrdered(byChild: "11").observe(.childAdded ,with: { (snapshot) in
if let post = snapshot.value as? [String : AnyObject] {
let img = Posts()
img.setValuesForKeys(post)
self.myarray.append(img)
self.tableView.reloadData()
}
})
}
正如El Capitain所说,您需要使用以下代码:
func loadData(){
let ref = FIRDatabase.database().reference().child("Posts")
ref?.queryOrdered(byChild: "Des").queryEqual(toValue: "11").observe(.childAdded, with: { snapshot in
if let post = snapshot.value as? [String : AnyObject] {
// do stuff with 'post' here.
}
})
}
另外,正如他提到的那样,您将需要设置安全规则,以允许您按“ Des”或您要查询的任何其他子节点进行搜索。 此外,您需要设置规则以允许您对查询位置进行读取访问:
{
"rules": {
".read": "auth != null",
".write": "auth != null",
"Posts": {
"$someID": {
".indexOn": ["Des"]
}
}
}
}
但是,您不会仅获得“图像”,查询将返回整个节点。 在这种情况下,上面的查询将返回rrrrrr节点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.