Swift DispatchQueue 与 Firebase 同步

标签 swift firebase firebase-realtime-database grand-central-dispatch

我试图在 for 循环中从 firebase 获取一些数据,但它不起作用。

我知道我应该使用 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 个 firebase 函数

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)
                    }
                })
            }
        }    
    }

**** 我的 firebase 函数之一的示例 ****

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")
            }
        }
    }

但我得到的是:

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

我试图在里面添加我的 for 循环:

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

最佳答案

我们这里没有足够的代码来理解应用程序的某些部分。但是,它似乎与您的 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 或一般的 google 搜索中有很多示例。

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

关于Swift DispatchQueue 与 Firebase 同步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45740008/

相关文章:

android - 使用 Firestore 时如何为 RecyclerView 添加搜索过滤器?

firebase - 如何在读取事件上触发firebase云功能

arrays - 试图将数组传递给另一个类的 Swift 错误

ios - 优化向 UIStackView 添加 View

ios - Swift 4、Firebase 5.8.0 FCM token 无

ios - Swift Firebase -如何验证 `PhoneAuthCredential` 并保持用户当前使用当前电子邮件 uid 登录

java - 从 android 中存储在 Firebase 中的 url 加载图像

firebase - 坚持为我的Firebase数据库设计架构

ios - Swift - 在 Collection View 中选择项目

ios - Apple 推送通知 (APN) 不一致?