swift - how to iterate and append Firebase data
问题描述
I have a table view that prints out comments.
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()
})
}
}
})
For every post available I want to look to see if it has a comment associated with it (nested comment) and if it does, append it to comments array.
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()
})
}
})
This is the attempted code to get the nested comments/ This is the pathway to get the nested comments. This is also what my JSON looks like.
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",
}}
解决方案
推荐阅读
- javascript - 如何根据 AngularJS 中的 url 显示/隐藏块
- android - 应用程序连接在服务器可以处理数据之前超时
- c# - 为什么未处理的异常技巧在所有情况下都不起作用
- sql - 如何计算 R 中 2 个时间戳之间的观察值(给出的示例)?
- c++ - 模板元编程 - 尝试实现维度分析
- python - 在 tensorflow2 中使用自定义数据生成器进行多线程处理
- java - Eclipse IDE 第一次没有打开
- vb.net - 使用 ComboBox 显示特定的 ImageList
- javascript - 在 css 覆盖后未启用 html 5 日期选择器中的键盘输入
- scala - 如何使用 Spark 改进 Kudu 读取?