2017-02-12 116 views
0

我想用firebase一次觀察多個數據,觀察塊保持循環直到它獲取所有數據。我需要知道它何時完成,以便我可以執行另一個塊。我怎樣才能做到這一點?我需要知道什麼時候完成Firebase觀察

databaseRef.child("First_Secondary_Grade").child("0").child("0").queryOrderedByKey().observe(.childAdded, with: { 
     (snapshot) in 

     if let dictoinary = snapshot.value as? [String: AnyObject] { 

      let dataofthequsation = structofthedata() 

      dataofthequsation.setValuesForKeys(dictoinary) 

      } 

    }) 

回答

0

我想我想通了

let databaseRef = FIRDatabase.database().reference() 
    var gotitall = 0 

//首先,你需要觀察單個事件讓孩子們真正的計數迅速觀察3將依靠孩子裏面的鑰匙。這就是爲什麼!

databaseRef.child("First_Secondary_Grade").child("0").child("0").observeSingleEvent(of:.value, with:{ (snap) in 
     gotitall = Int(snap.childrenCount) 
     databaseRef.child("First_Secondary_Grade").child("0").child("0").observe(.childAdded, with: { 
      snapshot in 
      if let dictoinary = snapshot.value as? [String: AnyObject] { 

       let dataofthequsation = structofthedata() 

       dataofthequsation.setValuesForKeys(dictoinary) 
       self.dataofthequsation.append(dataofthequsation) 

// this is will run when the block runs through all children 

       if gotitall == self.dataofthequsation.count { 
        completion() 
       } 
      } 
     }) 
    }) 
+0

如果您經過測試,它可以工作,您可以接受自己的答案。 –

+0

我會感謝你! –

相關問題