1

I have a problem with the following code not functioning...

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

}

When the images in the destinationViewController from the Array gameGroups get presented, only the last image in the array get presented. It iterates through the array imageUrl until the last element of the array, then the async-block gets executed. So far I tried with moving group1.enter() inside the completion but that didn't help either. Any ideas why this is happening? Did I misunderstand how to use DispatchGroup?

The Output of the above code is:

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

Any help would be appreciated! Thanks in advance!

0 Answers0