如何迭代和附加Firebase数据

时间:2018-08-03 04:10:55

标签: swift xcode uitableview firebase firebase-realtime-database

我有一个表格视图,可以打印出注释。

    Database.database().reference().child("main").child("posts").child(postID!).child("comments").queryOrdered(byChild: "id").observeSingleEvent(of: .value, with: { (snapshot:DataSnapshot) in
        if let postsDictionary = snapshot .value as? [String: AnyObject] {
            for testingkey in postsDictionary.keys {
                for post in postsDictionary {
                    //main comments
                    self.Comments.add(post.value)
                }
                DispatchQueue.main.async(execute: {
                    self.TableView.reloadData()
                })
            }
        }
    })

对于每个可用的帖子,我想查看它是否具有与之关联的评论(嵌套评论),如果有,请将其附加到评论数组。

Database.database().reference().child("main").child("posts").child(self.postID!).child("comments").child(testingkey).child("comments").queryOrdered(byChild: "post_id").observeSingleEvent(of: .value, with: { (snapshot:DataSnapshot) in
    if let postsDictionary = snapshot .value as? [String: AnyObject] {
        for post in postsDictionary {
            // nested comments
            self.Comments.add(post.value)
        }
        DispatchQueue.main.async(execute: {
            self.TableView.reloadData()
        })
    }
})

这是获取嵌套注释的尝试代码/这是获取嵌套注释的途径。这也是我的JSON的样子。

post-1069: {
    "comment-487" : {
      "id" : "comment-487",
      "content" : "blah blah blah",
      "comments" : {
        "comment-489" : {
          "id" : "comment-489",
          "content" : "i love you",
          "post_id" : "post-1069",
          "reply_to" : "comment-487",
        },
        "comment-490" : {
          "id" : "comment-490",
          "content" : "help me",
           "post_id" : "post-1069",
          "reply_to" : "comment-487",

}}

0 个答案:

没有答案