Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 包含异步调用的For循环_Swift_Grand Central Dispatch_Dispatch Async - Fatal编程技术网

Swift 包含异步调用的For循环

Swift 包含异步调用的For循环,swift,grand-central-dispatch,dispatch-async,Swift,Grand Central Dispatch,Dispatch Async,我有以下代码无法运行的问题 func getThatImage() { for imageurl in self.imageUrl { self.group1.enter() print("1:counter: \(counter)") self.dbFunctions.downloadImage(urlPath: imageurl, completion: { retrievedImage in

我有以下代码无法运行的问题

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)
    })

}
当显示destinationViewController中来自阵列
游戏组的图像时,仅显示阵列中的最后一个图像。它遍历数组
imageUrl
,直到数组的最后一个元素,然后执行异步块。到目前为止,我尝试在完成中移动
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

任何帮助都将不胜感激!提前谢谢

你碰巧解决了这个问题吗?我有完全相同的问题!是的,我做到了!你还需要它吗?你碰巧解决了这个问题吗?我有完全相同的问题!是的,我做到了!你还需要它吗?