1

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

2 Answers 2

1

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
    }
}
Sign up to request clarification or add additional context in comments.

7 Comments

Since comments are going to dynamically grow is there a way to download all the comments without having to do in manually by comment1, comment2 etc?
am working on it right now, will mark it up as soon as i get it working, thanks
Did it work your way? Or my way failed and than you tried your way?
this is what I have, it is not printing anything out: let comments = messages.childSnapshot(forPath: "comments").value as? [String: messageComments] if let comments = comments { for comment in comments.values { commentArray.append(comment) print("LOOK HERE") print(comment) } }
Why don't you try the solution I wrote there? Because you can't cast to [String: messageComments] . Test my solution and tell me how it goes.
|
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.