我在firebase中有三个节点,我想使用相同的循环循环。我已成功使用此代码遍历单个节点(cookie):
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)
}
}
})
我现在想要循环其余两个节点。任何想法?
我尝试添加其他节点,但控制台中的打印显示它仅从最后一个节点添加。请参阅以下代码:
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
})
})
})
答案 0 :(得分:1)
以下代码打印所有三个级别的快照。我希望这会有所帮助:
self.databaseRef.child("cookies").observeSingleEvent(of: .value, with: { (cookiesSnapshot) in
print("cookies snapshot: \(cookiesSnapshot)")
self.databaseRef.child("dessert").observeSingleEvent(of: .value, with: { (dessertSnapshot) in
print("dessert snapshot: \(dessertSnapshot)")
self.databaseRef.child("breakfast").observeSingleEvent(of: .value, with: { (breakfastSnapshot) in
print("breakfast snapshot: \(breakfastSnapshot)")
})
})
})
我无法检查这一点,所以我希望所有}和}都在正确的位置。 :)
答案 1 :(得分:1)
您可以将快照转换为dict并循环显示dict:
databaseRef.child("cookies").observeSingleEvent(of: .value, with: { snapshot in
if let dict = snapshot.value as? Dictionary<String, Dictionary<String, Any>> {
for (key, value) in dict {
if let recipeHeader = value["recipeHeaderFirebase"] as? String, favoriteArray.contains(key) {
self.numberOfRecipes.append(recipeHeader)
...
}
}
}
}
或者您可以使用枚举器:
databaseRef.child("cookies").observeSingleEvent(of: .value, with: { snapshot in
let enumerator = snapshot.children
while let (key, value) = enumerator.nextObject() as? FIRDataSnapshot {
if let recipeHeader = value["recipeHeaderFirebase"] as? String, favoriteArray.contains(key) {
self.numberOfRecipes.append(recipeHeader)
...
}
}
}