ios - 如何访问 childByAutoID 下的 Firebase 数据?

标签 ios swift firebase firebase-realtime-database

我目前正在尝试访问一些以随机子 ID 放入数据库的图书数据。我一直在寻找其他问题,但我得到的最远的是能够在闭包内访问 ID。我不知道如何正确设置完成处理程序来提取 ID。

另外,我不知道是否有更简单的方法来解决这个问题?

这是我尝试访问的数据: Firebase data

enter image description here

我认为这是我当前需要完成处理程序的代码?

func getChildID(department: String, course: String) {

        let parentRef = myDatabase.child("departments").child(department).child(course)

        parentRef.observeSingleEvent(of: .value) { (courseSnapshot) in
            if let data = courseSnapshot.value as? [String:String] {

                for value in data {
                    let isbn = value.value
                    if isbn != "ISBN" {

                        myDatabase.child("listings").child("\(isbn)").observeSingleEvent(of: .value, with: { (snapshot) in
                            let dictionary = snapshot.value as! NSDictionary
                            for key in dictionary.keyEnumerator() {
                                let myKey = key as! String
                                return
                            }
                        })
                    }
                }
            }
        }
    }

最佳答案

我们开始吧。问题的答案。然而,我在对这个问题的评论中提到,结构可以改进。但是,每个列出的子项下可能有更多的子项,因此这个答案可以处理它。

这只是打印每个作者的姓名,但它显示了如何到达它,因此应该为其他子节点扩展答案。

let listingsRef = self.ref.child("listings")
listingsRef.observeSingleEvent(of: .value, with: { snapshot in
    for child in snapshot.children {
        let autoIdSnap = child as! DataSnapshot
        for autoChild in autoIdSnap.children {
            let childSnap = autoChild as! DataSnapshot
            let dict = childSnap.value as! [String: Any]
            let author = dict["author"] as! String
            print(author)
        }
    }
})

哦,更好的结构可能是

listings
   childByAutoId
      author: "Some author"
      listing: 9780184888
   childByAutoId
      author: "Another author"
      listing: 9799292598

编辑:如果每个列表下只有一个且只有一个 childByAutoId,您可以消除上面的循环并执行此操作

let listingsRef = self.ref.child("listings")
listingsRef.observeSingleEvent(of: .value, with: { snapshot in
    for child in snapshot.children {
        let autoIdSnap = child as! DataSnapshot //each listing
        let childDict = autoIdSnap.value as! [String: Any] //a dict of items within the listing
        let firstKey = childDict.keys.first! //the key to the first item
        let valuesDict = childDict[firstKey] as! [String: Any] //the values for that key
        let author = valuesDict["author"] as! String //the author value
        print(author)
    }
})

关于ios - 如何访问 childByAutoID 下的 Firebase 数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51778915/

相关文章:

swift - TableView 不打印任何单元格(快速)

javascript - 防止 Firebase 云功能中的竞争条件

iphone - 在 iPhone 上检索当前本地时间?

ios - swift 4 中的“UIImage ?' is not convertible to ' UIImage”问题

ios - Swift:委托(delegate)协议(protocol)未正确设置 UILabel

Swift 3 UITableViewAction 错误在调用中缺少参数 rawValue 的参数

Swift:提供的 Facebook session token 已过期或无效

android - 使用 Cloud Functions for Firebase 发送推送通知

javascript - angularJS 将异步数据绑定(bind)到 ng-repeat orderBy

ios - 实例化 AVCaptureDeviceInput 时出错