swift - GCD + Firebase 云存储

标签 swift xcode grand-central-dispatch

有一个结构体,其中包含数组形式的结构体。为了填充核心,我必须先填充内部的,并将它们分配给主要的。为此,我使用 Dispatch Group () 和通知来添加和写入我将使用的主要结构。下面是我正在使用的代码。

但是由于这种方法我遇到了问题。通知的执行时间早于必要的时间。我在这里做错了什么?

这是控制台的输出:

DONE
[]
FBRecipe(name: "Eel kebab", count: "2", complexity: "3.75", time: "2", category: "Завтрак", type: "САЛАТЫ", about: "Lsvdvskld v\t", ingredient: [], cook: [], photo: [], idOwner: "XT2pgRnAZ8Q5pHH3dHsz5jYUZ613", shared: "0", planing: "0", timestamp: "1536761784.24662")
ingredinet
ingredinet
ingredinet

...

let loadRecipesGroup = DispatchGroup()
let loadItemsQueue = DispatchQueue(label: "ru.bryzgalov.cookbook.loadrecipes", qos: .userInteractive, attributes: [], autoreleaseFrequency: .workItem)

...

func loadRecipeList() {

    var recipe = [FBRecipe]()

    db.collection("RECIPES").getDocuments() { (querySnapshot, err) in
        if let err = err {
            print("Error getting documents: \(err)")
        } else {
            for documentRecipe in querySnapshot!.documents {
                self.loadItemsQueue.async {
                    var ingredinet = [FBIngredient]()
                    var stage = [FBStage]()
                    var photo = [FBDishPhoto]()

                    db.collection("RECIPES/\(documentRecipe.documentID)/INGREDIENT").getDocuments(completion: { (querySnapshot, err) in
                        if let err = err {
                            print("Error getting documents: \(err)")
                        } else {
                            for documentIngredient in querySnapshot!.documents {
                                self.loadItemsQueue.async(group: self.loadRecipesGroup) {
                                    let newIngredinet = FBIngredient(dict: documentIngredient.data() as Dictionary<String,AnyObject>)
                                    ingredinet.append(newIngredinet)
                                    print("ingredinet")
                                }
                            }
                        }
                    })

                    db.collection("RECIPES/\(documentRecipe.documentID)/STAGE").getDocuments(completion: { (querySnapshot, err) in
                        if let err = err {
                            print("Error getting documents: \(err)")
                        } else {
                            for documentStage in querySnapshot!.documents {
                                self.loadItemsQueue.async(group: self.loadRecipesGroup) {
                                    let newStage = FBStage(dict: documentStage.data() as Dictionary<String,AnyObject>)
                                    stage.append(newStage)
                                }
                            }
                        }
                    })

                    db.collection("RECIPES/\(documentRecipe.documentID)/PHOTO").getDocuments(completion: { (querySnapshot, err) in
                        if let err = err {
                            print("Error getting documents: \(err)")
                        } else {
                            for documentDishPhoto in querySnapshot!.documents {
                                self.loadItemsQueue.async(group: self.loadRecipesGroup) {
                                    let newDishPhoto = FBDishPhoto(dict: documentDishPhoto.data() as Dictionary<String,AnyObject>)
                                    photo.append(newDishPhoto)
                                }
                            }
                        }
                    })
                    self.loadRecipesGroup.notify(queue: .main) {
                        var newRecipe = FBRecipe(dict: documentRecipe.data() as Dictionary<String,AnyObject>)
                        newRecipe.ingredient = ingredinet
                        newRecipe.cook = stage
                        newRecipe.photo = photo
                        //                        recipe.append(contentsOf: newRecipe)
                        print(ingredinet)
                        print(newRecipe)
                    }
                }
                print("DONE")
            }
        }
    }
}

最佳答案

您的 done 会立即被调用,因为标记为 async 的调用是异步执行的。这意味着程序在执行这些调用时继续执行。一旦他们得到结果,他们就会完成并打印您期望的结果。因此,您的代码会运行每条语句直到最后。异步调用可能会在此之后完成一段时间。

关于swift - GCD + Firebase 云存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52312710/

相关文章:

ios - Swift 向后兼容 iOS 5 的版本

ios - 为 "Any iOS Device"构建 Swift 包时找不到 SwiftUI 或 Combine 类型

ios - 创建配置文件以与 TestFlight 一起使用?

objective-c - 在 ARC 之后,我应该为调度队列使用什么属性?

objective-c - 如何在 Swift 中将自定义 Objc 结构转换为 NSValue?

swift - NSManagedObject.entity() - 未初始化

objective-c - 如何在NSTimer循环中刷新TableView Cell数据

objective-c - NSWindow 附加到光标

iphone - iOS 应用程序的操作队列与调度队列

swift - 我如何在 Swift 3、Swift 4 及更高版本中使用 dispatch_sync、dispatch_async、dispatch_after 等?