CoderCody CoderCody - 3 months ago 14
iOS Question

Firebase Data Retrieval, Path Inside Path

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?

Answer

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.

Comments