Swift Firebase-如何在使用queryOrdered(byChild:).queryEqual(toValue:)时获取所有k/v [英] Swift Firebase -How to get all the k/v when using queryOrdered(byChild: ).queryEqual(toValue: )

查看:35
本文介绍了Swift Firebase-如何在使用queryOrdered(byChild:).queryEqual(toValue:)时获取所有k/v的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

root
  |
  @--reviews
        |
        |
        @--postABC // postId
               |
               |
               @--reviewXYZ // ***I want everything under this reviewUID
               |    |
               |    |--buyerUID: "01010"
               |    |--text: "fast shipping!"
               |    |
               |    @--responseTUV // responseUID
               |         |
               |         |--sellerUID: "212555"
               |         |--text: "we aim to please"
               |     
               @--reviewEFG // this review is from a totally different user I don't want anything from here

我的数据库如上所述进行设置.有时我只有买家uid,所以我使用 .queryOrdered(byChild:"buyerUID").queryEqual(toValue:"01010")提取数据,我可以从该特定帖子中找到买家,审查.

My database is setup like above. Sometimes I only have the buyer uid so to pull data I use .queryOrdered(byChild: "buyerUID").queryEqual(toValue: "01010") and I can locate the buyer from that specific post and it's review.

找到节点后,如何获取与该节点(reviewXYZ)下的买方关联的所有其他k/v?

let reviewsRef = Database.database().reference()?
                   .child("reviews")
                   .child("postABC")
                   .queryOrdered(byChild: "buyerUID")
                   .queryEqual(toValue: "01010")

reviewsRef?.observeSingleEvent(of: .value, with: { [weak self](snapshot) in

    print(snapshot.key) // prints  postABC
    print(snapshot.value) // prints a dictionary with reviewXYZ as the key and everything underneath it as a value

    guard let dict = snapshot.value as? [String: Any] else { return }
})

推荐答案

DataSnapshot 具有内置的方法来遍历其子级.

The DataSnapshot class has built-in methods to loop over its children.

reviewsRef?.observeSingleEvent(of: .value, with: { [weak self](snapshot) in
  for child in snapshot.children {
    let snap = child as! DataSnapshot;
    print(snap.key)
    print(snap.value) 
    print(snap.childSnapshot(forPath: "buyerUID").value)
  }
})

另请参阅其他搜索结果以遍历Firebase子节点在Swift .

这篇关于Swift Firebase-如何在使用queryOrdered(byChild:).queryEqual(toValue:)时获取所有k/v的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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