I have three nodes in firebase that I would like to loop through using the same loop. I'm successfully able to loop through a single node (cookies) using this code:
databaseRef.child("cookies").observeSingleEvent(of: .value, with: {(snapshot) in
for item in snapshot.children.allObjects as! [DataSnapshot] {
let thisItem = item.value as! NSDictionary
if(favoriteArray.contains(item.key)) {
self.numberOfRecipes.append(thisItem["recipeHeaderFirebase"] as! String)
let tempRecipe = Recipe()
tempRecipe.fbKey = item.key
tempRecipe.recipeHeaderObject = (thisItem["recipeHeaderFirebase"] as! String)
tempRecipe.recipeTextObject = (thisItem["recipeIngredientsTextFirebase"] as! String)
tempRecipe.recipeImageObject = (thisItem["recipeImageFirebase"] as! String)
self.recipeClassArray.append(tempRecipe)
}
}
})
I would now like to loop the two remaining nodes as well. Any Ideas?
I tried adding additional nodes, but a print in the console shows that it only adds from the last node. See code below:
self.databaseRef.child("cookies").observeSingleEvent(of: .value, with: {(snapshot) in
self.databaseRef.child("dessert").observeSingleEvent(of: .value, with: {(snapshot) in self.databaseRef.child("breakfast").observeSingleEvent(of: .value, with: {(snapshot) in
for item in snapshot.children.allObjects as! [DataSnapshot] {
let thisItem = item.value as! NSDictionary
if(favoriteArray.contains(item.key)) {
self.numberOfRecipes.append(thisItem["recipeHeaderFirebase"] as! String)
let tempRecipe = Recipe()
tempRecipe.fbKey = item.key
tempRecipe.recipeHeaderObject = (thisItem["recipeHeaderFirebase"] as! String)
tempRecipe.recipeTextObject = (thisItem["recipeIngredientsTextFirebase"] as! String)
tempRecipe.recipeImageObject = (thisItem["recipeImageFirebase"] as! String)
self.recipeClassArray.append(tempRecipe)
}
}
print(self.numberOfRecipes.count)
print(self.recipeClassArray)
let url = URL(string: self.recipeClassArray[self.currentView].recipeImageObject)
self.recipeImageUI.kf.setImage(with: url) //SÄTTER IN BILD MED KINGFISHER
self.recipeHeader.text = self.recipeClassArray[self.currentView].recipeHeaderObject //SÄTTER IN HEADER
self.ingredientText.text = self.recipeClassArray[self.currentView].recipeTextObject //SÄTTER IN TEXTEN
self.animateFunc(image: self.recipeImageUI, labelHeader: self.recipeHeader, labelText: self.ingredientText) //FUNKTION FÖR ATT ANIMERA
})
})
})