This question follows up from this: Join different nodes to observe them at once with firebase. I apologise if it is very similar but I am struggling to solve it in my project.
I have an IngredientData which obtains several ingredients and a property:
{
"IngredientData": {
"-KkY92MUZNHXxg4ivGY7": {
"name": "Onions"
},
"-KkY90e7dAgefc8zH3_F": {
"name": "Peas"
},
"-KkY9-HWJANdfhmD-SJ-": {
"name": "Mushrooms"
},
"-KkbWdomTYdpgEDLjC9w": {
"name": "Eggs"
},
"-KkbXOECSCBNAcVaO9rp": {
"name": "Rice"
}
}
}
I then have a RecipeData that contains recipe and inside that are ingredients.
{
"RecipeData": {
"-KlApwz5Y-TK_9z1sV1i": {
"recipeName": "Fried Rice",
"ingredients": {
"-KkbWdomTYdpgEDLjC9w": true,
"-KkbXOECSCBNAcVaO9rp": true,
"-KkY90e7dAgefc8zH3_F": true
}
}
}
}
I am now trying to get the details of the ingredients inside recipe so I can get the name property. I have a function which iterates inside the recipe's ingredient:
func fetchRecipeIngredients() {
let databaseRef = FIRDatabase.database().reference().child("RecipeData/recipe/-KlApwz5Y-TK_9z1sV1i/ingredients")
databaseRef.observeSingleEvent(of: FIRDataEventType.value, with: { snapshot in
for ingredients in snapshot.children {
print(snapshot)
}
})
}
Here is my Recipe
class:
class Recipe {
var name: String!
var key: String
init(from snapshot: FIRDataSnapshot) {
let snapshotValue = snapshot.value as! [String: Any]
self.name = snapshotValue["recipeName"] as! String
self.key = snapshot.key
}
}
I have two main issues:
Firstly, at the moment I am not sure how to iterate through a specific recipe or whether if that's necessary for what I want to achieve. For now I have hard-coded the recipeID to be able iterate inside it.
How do I get the details of the ingredients when using this
fetchRecipeIngredients()
Thanks in advance :)