为什么 DispatchGroup 无法正常工作
Why DispatchGroup is not working correctly
我希望函数先等待第一个函数从 firebase 获取数据,然后执行第二个函数,但事实证明它没有按正确的顺序工作
func getMenuData(){
getMenu()
getIngredient()
getRecipe()
self.dispatchGroup.notify(queue: .main){
print("finished download")
}
}
func getMenu(){
self.dispatchGroup.enter()
let menu = self.ref.collection("menu").document("menu1")
menu.getDocument(source: .cache) { (document, error) in
if let document = document {
let menuName = document.get("menuEngName") as! String
print("MenuName = \(menuName)")
} else {
print("Document does not exist in cache")
}
}
self.dispatchGroup.leave()
}
func getIngredient(){
for n in 1...5 {
print("getIngre")
}
}
func getRecipe(){
for n in 1...5 {
print("getRecipe")
}
}
原来在MenuName
之前打印了"finished download"
the result image
提前致谢
leave
行必须在里面完成闭包
func getMenu() {
self.dispatchGroup.enter()
let menu = self.ref.collection("menu").document("menu1")
menu.getDocument(source: .cache) { (document, error) in
if let document = document {
let menuName = document.get("menuEngName") as! String
print("MenuName = \(menuName)")
} else {
print("Document does not exist in cache")
}
self.dispatchGroup.leave()
}
}
我希望函数先等待第一个函数从 firebase 获取数据,然后执行第二个函数,但事实证明它没有按正确的顺序工作
func getMenuData(){
getMenu()
getIngredient()
getRecipe()
self.dispatchGroup.notify(queue: .main){
print("finished download")
}
}
func getMenu(){
self.dispatchGroup.enter()
let menu = self.ref.collection("menu").document("menu1")
menu.getDocument(source: .cache) { (document, error) in
if let document = document {
let menuName = document.get("menuEngName") as! String
print("MenuName = \(menuName)")
} else {
print("Document does not exist in cache")
}
}
self.dispatchGroup.leave()
}
func getIngredient(){
for n in 1...5 {
print("getIngre")
}
}
func getRecipe(){
for n in 1...5 {
print("getRecipe")
}
}
原来在MenuName
之前打印了"finished download"the result image
提前致谢
leave
行必须在里面完成闭包
func getMenu() {
self.dispatchGroup.enter()
let menu = self.ref.collection("menu").document("menu1")
menu.getDocument(source: .cache) { (document, error) in
if let document = document {
let menuName = document.get("menuEngName") as! String
print("MenuName = \(menuName)")
} else {
print("Document does not exist in cache")
}
self.dispatchGroup.leave()
}
}