我试图从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
你想通过在这里使用DispatchQueue来完成什么? Firebase SDK已经完成了主线程中的所有网络交互。它在主线程上执行的所有操作都会调用您的回调/完成处理程序。 –
问题是,从Firebase获得回调后,我从for循环中获得了值。当我得到firebase的回调,我做.. let index = self.getIndexOfExercice(exerciceName:exercice),但我总是得到“胸鳍飞行”作为exerciceName – Pierre
我在回调中添加了2更多的打印,让你更好地理解,我是什么好了。我添加了它们在控制台日志中显示的打印。 @Frank van Puffelen – Pierre