如何使Firebase数据库查询使用Swift 3来获得一些孩子? [英] How to make Firebase Database Query to get some of the children using Swift 3?

查看:71
本文介绍了如何使Firebase数据库查询使用Swift 3来获得一些孩子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Firebase数据库中进行查询以在控制台中获取某些子级?例如,从下面的快照中,如何进行查询以仅获取Image,其中Des: 11.

How can I make a Query in Firebase database to get some of the children in my console? For example, from the snapshot below, how can I make a query to get just the Images where 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所说,您需要使用此代码:

As El Capitain said, you'll need to use this:

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"或您要查询的任何其他子节点进行搜索.另外,您需要设置规则以允许您对查询位置具有读取权限:

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. 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"]
      }
    }
  }
}

但是,您不会仅获得图像",查询将返回整个节点.在这种情况下,上面的查询将返回rrrrrr节点.

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.

这篇关于如何使Firebase数据库查询使用Swift 3来获得一些孩子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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