I have an asynchronous task that is repeated a few times to pull up various image URLs for some animals. The code goes something like this:
let animals = ['Cheetah', 'Lion', 'Elephant']
let image_urls: [String:[String]] = [:]
for animal in animals {
var page = 0
var urls = []
while true {
let res = try await fetchImageUrls(animal, page)
if res.nextPage == nil {
break
}
urls.append(contentsOf: res.imagesUrls)
page = res.nextPage
}
image_urls[animal] = urls
}
The problem is there's a serial element to each task (need to load next page from call before) and then there's a parallel element (i can load animals in parallel)
When I do concurrentPerform
// .. same setup ..
DispatchQueue.concurrentPerform(iterations: self.animals.count) { (i) in
let animal = animals[i]
// same code as above:
var page = 0
var urls = []
while true {
let res = try await fetchImageUrls(animal, page)
// ...
}
}
it fails on the await. because concurrentPerform needs to remain synchronous... not sure how to fix this problem... adding completion handlers to make the code synchronous is very messy. Not sure how to fix this cleanly really... i don't know why fetchImageUrls
needs to be async but i also don't know enough to fix that
i feel lost here. any suggestions how to approach this?