如何使用Firebase数据库查询来获取使用Swift 3的一些孩子?

如何在Firebase数据库中创建查询以获取控制台中的一些子项? 例如,从下面的快照中,我如何进行查询以获得Des: 11Image

这是我在firebase数据库中的控制台的快照

我正在使用此代码:

  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节点。