I am trying to implement comments on posts on my app, but am having trouble getting the nested data called "comments" in the post. If my firebase database structure looks like the picture how can I properly download the comments as an array of messageComments?
func getFeedMessages(handler: @escaping (_ feedMessages:[FeedMessages]) -> ()){
var feedMessagesArray = [FeedMessages]()
var commentArray = [messageComments]()
REF_FEEDMESSAGES.observeSingleEvent(of: .value) { (feedMessagesSnapshot) in
guard let feedMessagesSnapshot = feedMessagesSnapshot.children.allObjects as? [DataSnapshot] else {return}
for messages in feedMessagesSnapshot {
let content = messages.childSnapshot(forPath: "content").value as? String ?? "Joe Flacco is an elite QB"
let icon = messages.childSnapshot(forPath: "icon").value as? String ?? "none"
let color = messages.childSnapshot(forPath: "color").value as? String ?? "bop"
let date = messages.childSnapshot(forPath: "date").value as? String ?? "0"
let comments = messages.childSnapshot(forPath: "comments").value as? [messageComments] ?? []
let userName = messages.childSnapshot(forPath: "userName").value as? String ?? "Anonymous"
let messages = FeedMessages(content: content, color: color, icon: icon, date: date, comments: comments, userName: userName)
feedMessagesArray.append(messages)
}
handler(feedMessagesArray)
}
}
