简体   繁体   English

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

[英]How to make Firebase Database Query to get some of the children using Swift 3?

How can I make a Query in Firebase database to get some of the children in my console? 如何在Firebase数据库中进行查询以在控制台中获取一些子级? For example, from the snapshot below, how can I make a query to get just the Image s where Des: 11 . 例如,从下面的快照中,如何进行查询以仅获取Image ,其中Des: 11

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

I'm using this code: 我正在使用此代码:

   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()

        }

    })

}

As El Capitain said, you'll need to use this: 正如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.

        } 
    })

}

Also, as he mentioned, you'll want to set your security rules to allow you to search by 'Des', or whatever other child node you'll be querying by. 另外,正如他提到的那样,您将需要设置安全规则,以允许您按“ Des”或您要查询的任何其他子节点进行搜索。 In addition, you'll need to set the rules to allow you read access to the query location: 此外,您需要设置规则以允许您对查询位置进行读取访问:

{
  "rules": {
    ".read": "auth != null",
    ".write": "auth != null",

    "Posts": {
      "$someID": {
        ".indexOn": ["Des"]
      }
    }
  }
}

However, you're not going to get 'only the images' though, your query will return the entire node. 但是,您不会仅获得“图像”,查询将返回整个节点。 In this case, the query above would return the rrrrrr node. 在这种情况下,上面的查询将返回rrrrrr节点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM