0

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.

Rob
441k74 gold badges848 silver badges1.1k bronze badges
asked Jul 30, 2019 at 13:35
1

1 Answer 1

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

answered Jul 30, 2019 at 13:47
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for this. Xcode is whining about Attribute can only be applied to types, not declarations with escaping param. How can I fix that?
you can remove @escaping and error will gone, but you will need to add this attribute when you will implement download code
The code compiles now just fine. However to the body of 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?
no, to get three outputs at a time you need to zip responses, after all code executed you will see "all images downloaded", async means that it is no matter when you will get any response, its like three cars on a road with different speed and you don't know who will finish first

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.