iOS Concurrency with GCD & Operations

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

Part 1: Grand Central Dispatch

10. Use a DispatchSemaphore

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: 09. Challenge: Download a Group of Images Next episode: 11. Concurrency Problems

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

A serial queue runs on a single thread, restricting access to a shared resource to one task at a time. Sometimes, you want to allow more threads, but not an unlimited number of threads. If you’re downloading data from the network, you might want to limit how many downloads happen at once, because you know the data is large and resource-heavy to process.

let semaphore = DispatchSemaphore(value: 4)
  for i in 1...10 { 
  queue.async(group: group) {
  
  } 
}
semaphore.wait()
defer { semaphore.signal() }
print("Downloading image \(i)")
// Simulate a network wait
Thread.sleep(forTimeInterval: 3) 
print("Image \(i) downloaded")
Downloading image 1
Downloading image 2
Downloading image 3
Downloading image 4
Image 4 downloaded
Image 2 downloaded
Image 1 downloaded
Image 3 downloaded
Downloading image 5
Downloading image 6
Downloading image 8
Downloading image 7
Image 6 downloaded
Image 5 downloaded
Image 8 downloaded
Image 7 downloaded
Downloading image 10
Downloading image 9
Image 9 downloaded
Image 10 downloaded
func dataTask_Group_Semaphore(with url: URL,
  group: DispatchGroup, semaphore: DispatchSemaphore,  // add semaphore here
  completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void) {
semaphore.wait()
group.enter()  // existing code
defer {
  group.leave()  // existing code
  semaphore.signal()
}
dataTask_Group_Semaphore(with: url, group: group, semaphore: semaphore) { data, _, error in
  if error == nil, let data, let image = UIImage(data: data) {
    images.append(image)
  }
}