私は同じループを使ってループしたいfirebaseに3つのノードを持っています。私はこのコードを使用して、単一ノード(クッキー)を介して正常にループすることができるよ:すべてのFirebaseの子供を同じループで一度にループするにはどうすればいいですか?
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)
}
}
})
今ループ残りの2つのノードに等も考えられます。何か案は?
追加のノードを追加しようとしましたが、コンソールの印刷で最後のノードからのみ追加されることが示されています。以下のコードを参照してください。
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
})
})
})
あなたはお互いに観察埋め込むことができます。たとえば、次のようなものがあります:databaseRef.child( "secondNode")のobserveSingleEventのobserveSingleEvent(of:.value、with:{これは技術的には1つのループではなく、データアクセスが次々に起こっています –
ありがとうございます、しかし、私はコンソールで印刷しましたが、最初の2つを無視して最後のノードから追加するようですリストアップされています...上記の私の投稿を更新してください:)何が欠けているのが見えますか? –
はい、印象が最も深いレベルでしか呼び出されていないので、そのようなものを試してください:self.databaseRef.child( "cookies")。observeSingleEvent .VALUEと:の{(cookiesSnapshot) \t \tプリント( "クッキー:\(cookiesSnapshot)")である。 \t \t self.databaseRef.child( "デザート")observeSingleEvent(の:.VALUE、と:{ (デザートSn apshot) \t \t \t印刷中( "デザート:\(dessertSnapshot)") \t \t \t self.databaseRef.child( "朝食")observeSingleEvent(の:。.VALUE、と:{(breakfastSnapshot) \t \tで\t print( "breakfast:\(breakfastSnapshot)") –