0
0
Swiftprogramming~10 mins

Task groups for parallel execution in Swift - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Task groups for parallel execution
Start Task Group
Add Tasks to Group
Run Tasks in Parallel
Wait for All Tasks to Finish
Collect Results
End Task Group
This flow shows how tasks are added to a group, run at the same time, and then results are collected after all finish.
Execution Sample
Swift
func fetchData() async {
  await withTaskGroup(of: Int.self) { group in
    group.addTask { await fetchNumber(1) }
    group.addTask { await fetchNumber(2) }

    var sum = 0
    for await number in group {
      sum += number
    }
    print("Sum: \(sum)")
  }
}
This code runs two tasks in parallel that fetch numbers, then sums and prints the result.
Execution Table
StepActionTask Group StateTask ResultOutput
1Start task groupEmpty group--
2Add task 1 (fetchNumber(1))Group with 1 task--
3Add task 2 (fetchNumber(2))Group with 2 tasks--
4Run tasks in parallelRunning 2 tasks--
5Task 1 completes1 task done, 1 running1-
6Task 2 completesAll tasks done2-
7Sum results (1 + 2)All tasks done-Sum: 3
8End task groupGroup closed--
💡 All tasks finished and results summed, task group ends.
Variable Tracker
VariableStartAfter Step 5After Step 6After Step 7Final
sum01333
group stateempty1 task doneall doneall doneclosed
Key Moments - 3 Insights
Why do tasks run at the same time instead of one after another?
Because tasks are added to a task group which runs them concurrently, as shown in steps 4 to 6 in the execution table.
How does the program know when all tasks are finished?
The for-await loop waits for each task result until all tasks complete, indicated by step 6 where all tasks are done.
Why is the sum variable updated inside the for-await loop?
Because each task result arrives asynchronously, the loop adds each number as it comes, shown in steps 5 and 6 updating sum.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'sum' after step 5?
A0
B1
C2
D3
💡 Hint
Check the variable_tracker row for 'sum' after step 5.
At which step does the task group have all tasks done?
AStep 4
BStep 5
CStep 6
DStep 7
💡 Hint
Look at the 'Task Group State' column in the execution_table.
If we add a third task returning 3, what would be the final sum after step 7?
A6
B5
C3
D4
💡 Hint
Sum is the total of all task results; adding 1 + 2 + 3 = 6.
Concept Snapshot
Task groups let you run many tasks at once.
Add tasks to the group with addTask.
Tasks run in parallel.
Use for-await to get results as they finish.
Waits until all tasks complete before continuing.
Full Transcript
This visual trace shows how Swift's task groups work for parallel execution. First, a task group starts empty. Then tasks are added to it. The group runs all tasks at the same time. As each task finishes, its result is collected. The sum variable adds each result inside a for-await loop. When all tasks finish, the final sum is printed. The task group then ends. This method helps run multiple jobs simultaneously and wait for all to complete before moving on.