0

I would like to know if it possible to access another path while within the completion block of a current path.

The way I am using it is as follows... I have a social media app, with a "Posts" path. That obviously is where I get all my information for the Posts. I would like to create "Comment(s)" for each post. That is where I would like there to be a path to "Comment(s)." What is someones recommendation for achieving this goal?

CoderCody
  • 103
  • 1
  • 9

2 Answers2

2

Here is sample code of how it should work. Using the same method in my projects. Of course, path is fake so fill it with your actual path.

// Read all posts from Firebase
ref.child("posts").observeEventType(.Value, withBlock: { snapshot in

        if let snapshots = snapshot.children.allObjects as? [FIRDataSnapshot] {

            // Loop through all posts
            for snap in snapshots {

                // Read comments for each post. snap.key in code below represents post key. Don't know if you have same structure so fill your data here.
                ref.child("comment").child(snap.key).observeEventType(.Value, withBlock: { snapshot in
                    if let postDictionary = snapshot.value as? Dictionary<String, AnyObject> {

                        // Here you have comments for current post

                    }
                })
            }
        }
    })
ZassX
  • 1,369
  • 2
  • 14
  • 35
1

I highly recommend first reading my classic answer on a similar question: Firebase data structure and url

Nesting data is in general discouraged in the Firebase Database. There have various reasons for that, but a few:

  • you can only retrieve a complete node. So if you nest the comments under each post, it means you will automatically get all comments whenever you are getting a post.

  • you often want different access rules on each of these types (posts vs comments). This is more difficult to manage when you nest them, since permission trickles down.

I would have three top-level lists: posts and comments.

posts
    $postid
        author: "uidOfCoderCody"
        title: "Firebase Data Retrieval, Path Inside Path" 
        body: "I would like to know if it possible to..."
comments
    $postid
        $commentid
            author: "uidOfZassX"
            comment: "Here is sample code of how it should work."

Since the comments are stored under the same $postid of the post itself, you can easily look up the comments for a post.

Depending on the use-cases that your app covers, you'll need to adapt or (more likely) expand this data model to efficiently allow your use-cases. For learning more, I also recommend reading this article on NoSQL data modeling.

Community
  • 1
  • 1
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • I like your strategy better. I'm guessing I can access the autoID of a post using dot notation? – CoderCody Aug 31 '16 at 18:07
  • I'm not sure what you mean by that. You can definitely get the autoID/key of the posts. After you call `let newPostRef = ref.childByAutoId()`, you can get it by `newPostRef.key`. – Frank van Puffelen Aug 31 '16 at 18:50
  • @CoderCody my answer below is how to read from Firebase if structure is like this one above. – ZassX Sep 01 '16 at 12:13