iOS Concurrency with GCD & Operations

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

Part 1: Grand Central Dispatch

07. Use a Group of Tasks

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: 06. The Right Way to Download Images Next episode: 08. Wrap an Asynchronous Function

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

Use Group of Tasks

Open UseGroupOfTasks.playground in the starter folder. In Sources, there’s a SlowSum file with a slowAdd(_:) function. You’ll create a group of slowAdd tasks and run them on userQueue:

let userQueue = DispatchQueue.global(qos: .userInitiated)
let numberArray = [(0,1), (2,3), (4,5), (6,7), (8,9)]
let slowAddGroup = DispatchGroup()
for inValue in numberArray {
  userQueue.async(group: slowAddGroup) {  // pause to point out group: method
    let result = slowAdd(inValue)
    print("Result = \(result)")
  }
}
slowAddGroup.notify(queue: mainQueue) {  // indicate queue:work: method
  print("SLOW ADD: Completed all tasks")
  sleep(3)
  PlaygroundPage.current.finishExecution()
}
=== Group of sync tasks ===

Result = 9
Result = 17
Result = 13
Result = 1
Result = 5
SLOW ADD: Completed all tasks

DispatchGroup Waiting

If the current queue really can’t do anything until the group finishes, you can call wait. Unlike waiting for a dispatch work item, which promotes the priority of the queue you dispatched the work item to, the dispatch system doesn’t promote any of the queues used by group tasks. The only reason I can think of to wait for a group is to free up the current thread, so the group tasks can use it.

let group = DispatchGroup()
let queue = DispatchQueue.global(qos: .userInitiated)

queue.async(group: group) {
  print("Start task 1")
  print("End task 1")
}

queue.async(group: group) {
  print("Start task 2")
  print("End task 2")
}
sleep(4)  // between print statements
sleep(1)  // between print statements
print("All tasks completed at last!")
sleep(1)
PlaygroundPage.current.finishExecution()
Start task 1
Start task 2
End task 2
End task 1
All tasks completed at last!
if group.wait(timeout: .now() + 5) == .timedOut {
  print("I got tired of waiting.")
} else {
  print("All the tasks have completed.")
}
Start task 1
Start task 2
End task 2
End task 1
All tasks completed at last!
All the tasks have completed.
Start task 1
Start task 2
End task 2
I got tired of waiting.
End task 1
All tasks completed at last!