To greatly simplify the question, say I have a Swift array consisting of three image URLs that I would like to download like so:
let urls:[String] = [
"http://acme.com/one.jpeg",
"http://acme.com/two.jpeg",
"http://acme.com/three.jpeg",
]
for url in urls {
downloadImage(url)
}
print("all images downloaded.")
What if I would like to download all of the files in parallel? After reading about Grand Central Dispatch (GCD) and async programming in Swift I'm still unsure how to solve that "problem". I do not want to modify the array, all I want to achieve is parallel execution of downloadImage(url)
tasks.
Thanks in advance.
-
Maybe this can help a little bit stackoverflow.com/questions/50042458/…Savca Marin– Savca Marin2019年07月30日 13:42:19 +00:00Commented Jul 30, 2019 at 13:42
1 Answer 1
I would advice you to use DispatchGroup for it, I don't know how you will download your images but example of code will look like
private func downloadAll() {
let urls:[String] = [
"http://acme.com/one.jpeg",
"http://acme.com/two.jpeg",
"http://acme.com/three.jpeg",
]
let group = DispatchGroup()
for url in urls {
group.enter()
downloadImage(url) {
group.leave()
}
}
group.notify(queue: .main) {
print("all images downloaded")
}
}
func downloadImage(_ url: String, @escaping block: () -> ()) {
// your code to download
// in completion block call block()
// it will call block in for loop to leave the group
}
Hope it will help you, to download you can use SDWebImage framework, it is quite easy in usage
4 Comments
Attribute can only be applied to types, not declarations
with escaping
param. How can I fix that?downloadImage
I added sleep(3)
and print(url)
before calling the block
callback. The result is that I see printing in the console every three seconds. If that is truly async I should see all three console outputs after three seconds, right?