How do I associate a firebase sub-entry to a main entry? (explanation in comments)

Question!

The JSON:

{
  "coffee-shops" : {
    "-KJ4I4D-Jrqrzc9wP42C" : {
      "coffeeShopName" : "Starbucks",
      "coffeeShopRating" : 3.5,
    "-KJ4VVB51wx9NpEKtjxQ" : {
      "coffeeShopName" : "Starbucks",
      "coffeeShopRating" : 1,
      "coffeeShopReview" : "R",
      "coffeeShopReviewerName" : "Charles"
    },
    "-KJEJ6MpQwOHcay_9k6v" : {
      "coffeeShopName" : "Starbucks",
      "coffeeShopRating" : 4,
      "coffeeShopReview" : "B",
      "coffeeShopReviewerName" : "Charles"
    }
  }
  },
  "users" : {
    "02a54e06-9635-4e22-9bb7-c0ddcd9c6f4f" : {
      "email" : "[email protected]",
      "provider" : "password",
      "username" : "thecoffeeguy"
    },
    "03fe2c17-3c66-442c-a63d-4a1e02fd660c" : {
      "email" : "[email protected]",
      "provider" : "password",
      "username" : "Charles"
    },
    "16a7279f-5478-4f3f-b5f8-2f261d166d92" : {
      "email" : "[email protected]",
      "provider" : "password",
      "username" : "haha"
    },
    "23275f65-8e16-4ede-9236-21485b7493b9" : {
      "email" : "[email protected]",
      "provider" : "password",
      "username" : "boo"
    },
    "a5ed6962-76bc-476a-b432-6787e45badfc" : {
      "email" : "[email protected]",
      "provider" : "password",
      "username" : "mesbekmek"
    }
  }
}

Some context: I'm making a coffee app and I need to have the reviews be specific to the coffee shop a user is at. Right now, all reviews that have ever been made will show up on my tableview.

This isn't so much a "how to code" question, but me wondering how to approach this and how I might solve it.

This is what I think I should do:

  1. get a specific coffee shop's uuid

  2. iterate over the reviews because they are sub-entries in my coffee shop model

  3. get data from iterating over the reviews, then see if the uuid of the cell(?) selected matches the review uuid

This doesn't really sound right to me, so any help would be great.



Answers
You can organize your db in a different way.

coffee-shops:
|--coffeeId1
|--coffeeId2
|--coffeeId3

reviews:
|--coffeeId1
|----reviewId1
|----reviewId2
|--coffeeId2
|----reviewIdX
|----reviewIdXx

When you insert a review you can use the same key of the coffee-shop.
In this way all the reviews of the same coffee shop is under the same ref.

You can achieve it using something like this:

ref(reviews).child(coffeeshop.getKey()).push();


This video can help you solving your question :)
By: admin