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