iOS Concurrency with GCD & Operations

Sep 12 2023 · Swift 5.8, macOS 13, iOS 16, Xcode 14.3

Part 1: Grand Central Dispatch

05. Challenge: A Better Way to Download Images

Episode complete

Play next episode

Next
About this episode
Leave a rating/review
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 04. Use Dispatch Work Items Next episode: 06. The Right Way to Download Images

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Challenge: A Better Way to Download Images

In the first episode, I showed you the sample app for this course. It’s unresponsive to user interaction because the image downloads all happen on the main thread, which should only be used for user interface tasks.

[weak self] in
  guard let self else { return }
store.downloadImageOffMainQueue(index: image.id)  // change On to Off
DispatchQueue.global(qos: .utility).async { 
}
DispatchQueue.global(qos: .utility).async { [weak self] in
  guard let self else { return }
}
if let data = try? Data(contentsOf: self.images[index].url),
  let decodedImage = UIImage(data: data) {
  images[index].image = decodedImage
}
DispatchQueue.main.async {
  self.images[index].image = decodedImage
}