Swift 如何在同一个循环中同时循环所有Firebase子级?

Swift 如何在同一个循环中同时循环所有Firebase子级?,swift,firebase,firebase-realtime-database,Swift,Firebase,Firebase Realtime Database,我在firebase中有三个节点,我想使用相同的循环来循环。我能够使用以下代码成功地循环通过单个节点(cookies): databaseRef.child("cookies").observeSingleEvent(of: .value, with: {(snapshot) in for item in snapshot.children.allObjects as! [DataSnapshot] { let thisItem = item.value

我在firebase中有三个节点,我想使用相同的循环来循环。我能够使用以下代码成功地循环通过单个节点(cookies):

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     
    }) 
  })  
})

下面的代码打印所有三个级别的快照。我希望这将有助于:

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)")
        })
    })
})

我不能检查这个,所以我希望所有的}和)都在正确的位置。:)

您可以将快照转换为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)在databaseRef.child(“secondNode”).observeSingleEvent(of:.value,with:{(snapshot)在…中等等。从技术上讲,这不是一个循环,而是一个接一个的数据访问。谢谢,但我在控制台中进行了打印,它似乎只是从最后一个节点添加的,忽略了前面列出的两个…更新我上面的帖子:)你能看到缺少什么吗?是的,因为print只在最深层调用…所以请尝试这样的操作:self.databaseRef.child(“cookies”).observeSingleEvent(of:.value,with:{(cookiesSnapshot)in print(“cookies:(cookiesSnapshot)”)self.databaseRef.child(“甜点”).observeSingleEvent(of:.value,with:{(甜点快照)打印(“甜点:(甜点快照)”)self.databaseRef.child(“早餐”).observeSingleEvent(of:.value,带有:{(早餐快照)打印(“早餐:(早餐快照)”)真棒的人,谢谢你的帮助!:)谢谢你的快速回复!我会尝试一下,试用完后再回来!:)
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)
            ...
        }
    }
}