ios - 如何使 Firebase 数据库查询让一些 child 使用 Swift 3?

标签 ios firebase swift3 firebase-realtime-database

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

This is a snapshot for my console in firebase database

我正在使用这段代码:

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

关于ios - 如何使 Firebase 数据库查询让一些 child 使用 Swift 3?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41606963/

相关文章:

ios - 如何从 nsdate(timeintervalsince1970) 创建时间戳

Swift 3 泛型 : issue with setting UICollectionViewDatasource & Delegate for a UICollectionView inside UITableViewCell

ios - 多个 UNUserNotifications 未触发

javascript - 当某个事件触发时,Javascript 可以在 iOS 中调用任何东西吗?

ios - 如何从 Swift 中的 TableCell 类访问 UIViewController 类中的值?

android - Firebase 应用崩溃

json - 如何使用 Core Data 保存 JSON 响应,在 Swift 3 中互联网离线时显示数据?

swift - 嵌套调度组 Swift

ios - 在模拟器中运行应用程序时删除本地通知的 iOS 权限警报

android - 无法在 firebase 上更新一个字段 - Android