2017-02-12 103 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

我会感谢你! –

相关问题