FIRDatabaseQuery: how to do an inner join

浪尽此生 提交于 2019-12-01 09:10:15
adolfosrs

You will need to work with nested firebase calls. You can find a javascript example in this question but your swift code should look like the following:

ref.child("posts").observeEventType(.ChildAdded, withBlock: { (snapshot) in
    if let postId = snapshot.key as! String {
      let commentsRef = ref.child("post-comments")  
      commentsRef.child(postId).queryOrderedByChild("uid").queryEqualToValue(userId).observeSingleEventOfType(.Value, withBlock: { (snapshot) in
            for child in snapshot.children.allObjects as [FDataSnapshot] {
               print(child.value)     
            }
        }) { (error) in
            print(error.localizedDescription)
        }
      }
})

You may want to consider

"posts" : {
    "-KIycKhZU55dmKnHbbxv" : {
      "author" : "John Doe",
      "body" : "This is a post test",
      "title" : "test",
      "uid" : "SHaH36BLwgPvwgi9cDmRnsnONFB2"
      "commented_by"
           "SHaH36BLwgPvwgi9cDmRnsnONFB2": true

Then you can simply query for

posts.child("commented_by/uid").queryEqualToValue(true)

or, change the post-comments around to better match the queries you want to do:

"post-comments" : {
  "-KIycMyL0Vy1BHVdI4zc" : {
     "author" : "toto",
     "post_data":
          post_id: "-KIycKhZU55dmKnHbbxv",
          post_title: "test"
     "text" : "test",
     "uid" : "SHaH36BLwgPvwgi9cDmRnsnONFB2"
},

That makes the query a snap as the post-comments node can be queried for the uid which return all of the post_id's of the posts they commented on. It wouldn't return the post itself, however, you may just be looking for the title so you can populate a list. Once the user taps it clicks it you can retrieve the actual data.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!