Noah Iarrobino
Noah Iarrobino

Reputation: 1543

Getting nested data in firebase using swift

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)
        }
    }

enter image description here

Upvotes: 1

Views: 736

Answers (2)

Noah Iarrobino
Noah Iarrobino

Reputation: 1543

here is the solution

let comments = messages.childSnapshot(forPath: "comments").value as? [String: Any] ?? [:]



                for comment in comments {

                    let theComment = comment.value as? [String: Any]

                    let theContent = theComment?["content"] as? String ?? ""
                    let theIcon = theComment?["icon"] as? String ?? ""
                    let theColor = theComment?["color"] as? String ?? ""
                    let theDate = theComment?["date"] as? String ?? ""
                    let theName = theComment?["userName"] as? String ?? ""

                    let aComment = messageComments(content: theContent, color: theColor, icon: theIcon, date: theDate,  userName: theName)
                    commentArray.append(aComment)
                }

Upvotes: 0

DionizB
DionizB

Reputation: 1507

You cannot access comments like that. You need either to use encodable to do that or since you're accessing values inside every snapshot manually you can access it like this:

let comments = messages.childSnapshot(forPath: "comments").value as? [String: Any]
let comment1 = comments?["comment1"] as? String ?? "comment1"
let comment2 = comments?["comment2"] as? String ?? "comment2"

Then you need to initialize your object messageComments normally by calling your messageComments initializer. Also I would recommend starting your class names with Capital letter.

Edit: for loading comments manually I would recommend this:

if let comments = comments { 
    for comment in comments.values {
       // here your access your comment as you want, you need to cast as string
    }
}

Upvotes: 1

Related Questions