2017-08-17 127 views
0

我试图从for循环中的firebase中获取一些数据,但它不起作用。与Firebase同步的Swift DispatchQueue

我知道我应该使用DispatchQueue,但我无法理解我应该如何使用它与Firebase。

我有一个循环:

for i in 0..<Exercice.workout.count{ 
    exercice = (Exercice.workout[i] as! [[Any]])[0][0] as! String 
    print("Exercice: \(exercice)") 
    self.endDeleteLastSerie(key: key, exercice: exercice, callback: {(status, endTime) in 
     if status == "success"{ 
      print("do stuff here") 
      let index = self.getIndexOfExercice(exerciceName: exercice) 
      print("Index: \(index)") 
      print("ExerciceName: \(exercice)") 
     } 
} 

在我的功能endDeleteLastSerie我打电话2个火力功能

func endDeleteLastSerie(key:String, exercice: String, callback: @escaping(_ status:String, _ endTime: Int)->Void){ 
     FirebaseWorkout.deleteSerie(key: key, exercice: exercice) { (status) in 
      if status == "success" { 
       //we set the end time to firebase 
       FirebaseWorkout.updateEndExercice(exercice: exercice, callback: { (status, endTime) in 
        if status == "success" { 
         callback("success", endTime) 
        }else{ 
         callback("error", endTime) 
        } 
       }) 
      } 
     }  
    } 

****我火力功能的一个实例****

static func deleteSerie(key: String, exercice: String, callback: @escaping (_ status: String)->Void){ 
     let uid = FirebaseUsers.User.uid 
     print("remove") 
    Database.database().reference().child("users/"+uid+"/workout/"+self.workoutKey+"/exercice/"+exercice+"/series/"+key).removeValue { (error, DatabaseReference) in 
      if error == nil { 
       print("removed from firebase") 
       callback("success") 
      }else{ 
       callback("error") 
      } 
     } 
    } 

但我“m到处是:

Exercice: Bench Press 
remove 
Exercice: Pectoral Fly 
remove 
removed from firebase 
removed from firebase 
do stuff here 
Index: 1 
ExerciceName: Pectoral Fly 
do stuff here 
Index: 1 
ExerciceName: Pectoral Fly 

我试图加我为循环内:

DispatchQueue.main.sync { } 

DispatchQueue.global().sync(execute: { }) 

var _dispatchQueue:DispatchQueue = DispatchQueue(label: "first", qos: .userInteractive) 

那里面加我的for循环

self._dispatchQueue.sync { } 

但是,没有什么工作

我怎样才能解决这个问题?并获得

Exercice: Bench Press 
remove 
removed from firebase 
do stuff here 
Index: 0 
ExerciceName: Bench Press 

Exercice: Pectoral Fly 
remove 
removed from firebase 
do stuff here 
Index: 1 
ExerciceName: Pectoral Fly 
+0

你想通过在这里使用DispatchQueue来完成什么? Firebase SDK已经完成了主线程中的所有网络交互。它在主线程上执行的所有操作都会调用您的回调/完成处理程序。 –

+0

问题是,从Firebase获得回调后,我从for循环中获得了值。当我得到firebase的回调,我做.. let index = self.getIndexOfExercice(exerciceName:exercice),但我总是得到“胸鳍飞行”作为exerciceName – Pierre

+0

我在回调中添加了2更多的打印,让你更好地理解,我是什么好了。我添加了它们在控制台日志中显示的打印。 @Frank van Puffelen – Pierre

回答

0

我们没有足够的代码来理解应用程序的某些部分。但是,它似乎与您的Firebase代码无关。 Exercise.workout数组是如何管理的?

在你的第一个代码片段:

for i in 0..<Exercice.workout.count{ 
    exercice = (Exercice.workout[i] as! [[Any]])[0][0] as! String 
    print("Exercice: \(exercice)") 
    self.endDeleteLastSerie(key: key, exercice: exercice, callback: {(status, endTime) in 
     if status == "success"{ 
      print("do stuff here") 
      let index = self.getIndexOfExercice(exerciceName: exercice) 
      print("Index: \(index)") 
      print("ExerciceName: \(exercice)") 
     } 
} 

的for循环是通过Exercise.workout阵列运行,您引用的功能似乎并没有被去除的数组项。当然,这可能在代码的其他地方。另外,我们不知道getIndexOfExercice函数是如何工作的。

此外,您正在处理不同线程上的机箱,因此打印语句可能以各种顺序发生。如果你想让这些同步发生,你需要添加代码来做到这一点。

有很多同步线程的方法,并且有很多StackOverflow或一般谷歌搜索的例子。

但是,我不认为这是你的问题的症结所以如果这没有帮助你找到问题,你将需要围绕正在使用的数组提供更多的代码(和getindex函数)。