2017-02-12 7 views
0

firebaseを使用して複数のデータを一度に観測しようとしていますが、観測ブロックはすべてのデータをフェッチするまでループし続けます。私はそれが実際に行われたときに別のブロックを実行できるようにする必要があります。どうやってやるの?私はfirebase観測が完全に完了したときを知る必要があります

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

それを考え出したと思う

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

//あなたが観察SWIFT 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

私はあなたに感謝します! –

関連する問題