gpt4 book ai didi

swift - GCD + Firebase 云存储

转载 作者:行者123 更新时间:2023-11-30 11:09:34 24 4
gpt4 key购买 nike

有一个结构体,其中包含数组形式的结构体。为了填充核心,我必须先填充内部的,并将它们分配给主要的。为此,我使用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com