1

I have nested data that I'd like to display in a tableView.

My data is structured like so...

/users
    /userid
         name: "John"
         age: 23
         /likedPosts
            0:post1
            1:post2

For the tableview I'd like to display these posts (which have their own collection of data).

In order to do that I need to...

1) Get the count of the array and

2) Query the users likedPost array values to get the content of the post.

I'm currently using the getDocument function and can't figure it out.

for example...

func getUserLikedPosts() {
    if let user = Auth.auth().currentUser {
        let userFS = Firestore.firestore().collection("users").document(user.uid)
        userFS.getDocument(completion: { (snapshot, error) in
            print(snapshot?)

        })
    }
}

This doesn't even print out the nested array?

0

1 Answer 1

4

Something like this should work:

func getUserLikedPosts() {
if let user = Auth.auth().currentUser {
    let userFS = Firestore.firestore().collection("users").document(user.uid).collection(“likedPosts”)
    userFS.getDocuments(completion: { (snapshot, error) in
        print(snapshot?)

    })
}
}

With Firestore you need to drill down to the actual node you want, unlike Firebase where you can access child snapshots.

So after a quick discussion, with the OP, it was that the user in question didn’t have a likedPost object.

To access the array (Please be aware I haven’t tested this code, it is an example):

If let doc = document, let array = doc[“likedPost] as NSArray {
print(array)
}
Sign up to request clarification or add additional context in comments.

6 Comments

It isn't a collection. It's an array in the document.
Ahh I see. Does it print out the rest of the data?
No it says nil. If I print it out the way i was doing it - it shows all of the top level data ... ie name & age. But doesn't print out even the header "likedPosts"
Really daft, but have you checked that the user you are trying to access has likedPosts as an object in its hierarchy?
Good catch! haha so there wasn't anything for the user...but still nothing with both of the ways we were printing...i found if we do if let userInfo = document { print(userInfo.data()) } THEN is shows up in the console as....<__NSArrayM 0x1c0c5f050>( faskdjfl, asdf ) - the gibberish is my quick making a post ;)
|

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.