我遇到以下代码无法正常运行的问题...
func getThatImage() {
for imageurl in self.imageUrl {
self.group1.enter()
print("1:counter: \(counter)")
self.dbFunctions.downloadImage(urlPath: imageurl, completion: { retrievedImage in
print("2:counter: \(self.counter)")
self.gameGroups[self.counter].icon = retrievedImage.0!
print(self.gameGroups[self.counter].iconUrl)
self.counter += 1
print("3:counter: \(self.counter)")
self.group1.leave()
})
}
self.group1.notify(queue: DispatchQueue.main, execute: {
self.performSegue(withIdentifier: "toNavController", sender: self)
})
}
当呈现来自数组gameGroups
的destinationViewController中的图像时,仅呈现数组中的最后一个图像。它遍历数组imageUrl
直到数组的最后一个元素,然后执行async-block。到目前为止,我尝试在完成中移动group1.enter()
,但这也没有帮助。任何想法为什么会这样?我是否误解了如何使用DispatchGroup
?
上述代码的输出为:
1:counter: 0
1:counter: 0
1:counter: 0
2:counter: 0
Optional("static/number-3-256.png")
3:counter: 1
2:counter: 1
Optional("static/number-3-256.png")
3:counter: 2
2:counter: 2
Optional("static/number-3-256.png")
3:counter: 3
任何帮助将不胜感激!提前谢谢!