2017-06-19 79 views
0

我有五种观察方法,它们从Firebase获取五个不同的INT值。所有的脚本实际上都在工作。如果我打印快照,控制台会显示五个不同的值。在五种观察方法之外,我有一个连接到条形图的数组。我想取第一个值并将其附加到数组的[0]中,依此类推。 这是我的代码:Firebase观察并获取数组的值

let myArray = [1, 2, 3, 4, 5] 

    var exampleString = (label.text?.lowercased())! 
    ref2 = FIRDatabase.database().reference() 
    ref2.child("parole_chiave").child(exampleString).child(exampleString).child("valore1").observe(.value, with: { snapshot in 

     print(snapshot.value!) 

    }) 
    ref2.child("parole_chiave").child(exampleString).child(exampleString).child("valore2").observe(.value, with: { snapshot in 

     print(snapshot.value!) 

    }) 
    ref2.child("parole_chiave").child(exampleString).child(exampleString).child("valore3").observe(.value, with: { snapshot in 

     print(snapshot.value!) 

    }) 
    ref2.child("parole_chiave").child(exampleString).child(exampleString).child("valore4").observe(.value, with: { snapshot in 

     print(snapshot.value!) 

    }) 
    ref2.child("parole_chiave").child(exampleString).child(exampleString).child("valore5").observe(.value, with: { snapshot in 

     print(snapshot.value!) 

    }) 

回答

1

一种可能的方式来实现你的目标是窝在彼此内部观察,这将迫使第一观察完成,并填充阵列,第二观察发生之前。

ref2.child("parole_chiave").child(exampleString).child(exampleString).child("valore1").observe(.value, with: { snapshot in 

myArray.append(snapshot.value) as! Int 
ref2.child("parole_chiave").child(exampleString).child(exampleString).child("valore2").observe(.value, with: { snapshot2 in 

    myArray.append(snapshot2.value) as! Int 
    ref2.child("parole_chiave").child(exampleString).child(exampleString).child("valore3").observe(.value, with: { snapshot3 in 

     myArray.append(snapshot3.value) as! Int 
     ref2.child("parole_chiave").child(exampleString).child(exampleString).child("valore4").observe(.value, with: { snapshot4 in 

      myArray.append(snapshot4.value) as! Int 
      ref2.child("parole_chiave").child(exampleString).child(exampleString).child("valore5").observe(.value, with: { snapshot5 in 

       myArray.append(snapshot5.value) as! Int 

      }) 

     }) 

    }) 

}) 

请注意,您将不得不更改每个快照的名称,因为它们现在处于相同的方法中。