A task group lets you run multiple tasks in parallel and wait for all of them to finish. It's like organizing a team to do jobs at the same time and then gathering their results.
Click to reveal answer
beginner
How do you add a new task to a Swift task group?
Inside the task group, you use the addTask method to start a new child task that runs concurrently with others.
Click to reveal answer
beginner
What does the await keyword do when used with a task group?
It waits for all tasks in the group to finish and collects their results before moving on, ensuring you have all the data ready.
Click to reveal answer
beginner
Why use task groups instead of running tasks one by one?
Task groups let tasks run at the same time, which can make your program faster, especially when tasks don't depend on each other.
Click to reveal answer
intermediate
What happens if one task in a task group throws an error?
The error is thrown to the caller, and the task group cancels remaining tasks. You can handle errors using try and catch.
Click to reveal answer
What is the main benefit of using task groups in Swift concurrency?
ARun tasks strictly one after another
BAutomatically fix errors in tasks
CPrevent any task from running concurrently
DRun multiple tasks in parallel and wait for all to finish
✗ Incorrect
Task groups allow multiple tasks to run at the same time and wait for all to complete.
Which method do you use to add a new task inside a Swift task group?
ArunTask
BstartTask
CaddTask
DcreateTask
✗ Incorrect
The addTask method is used to add new child tasks to a task group.
What keyword do you use to wait for all tasks in a task group to finish?
Aawait
Bsync
Cpause
Dwait
✗ Incorrect
The await keyword pauses execution until all tasks in the group complete.
If one task in a task group throws an error, what happens?
AThe error is ignored
BThe task group cancels remaining tasks and throws the error
CAll tasks continue running without interruption
DThe program crashes immediately
✗ Incorrect
When a task throws an error, the task group cancels other tasks and propagates the error.
Why might you choose task groups over manually creating multiple tasks?
ATask groups automatically handle task cancellation and error propagation
BTask groups run tasks sequentially
CTask groups prevent concurrency
DTask groups slow down execution
✗ Incorrect
Task groups help manage tasks better by handling cancellation and errors automatically.
Explain how task groups help run tasks in parallel in Swift concurrency.
Think about how a group of friends can work on different parts of a project at the same time.
You got /4 concepts.
Describe what happens when a task inside a task group throws an error.
Consider what happens if one team member finds a problem during a group project.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of using a TaskGroup in Swift concurrency?
easy
A. To run multiple tasks in parallel and wait for all to complete
B. To create a single long-running task
C. To pause the current task indefinitely
D. To handle errors in synchronous code
Solution
Step 1: Understand what TaskGroup does
A TaskGroup lets you run many tasks at the same time, managing them together.
Step 2: Identify the main use
It allows parallel execution and waits until all tasks finish before continuing.
Final Answer:
To run multiple tasks in parallel and wait for all to complete -> Option A
Quick Check:
TaskGroup = parallel tasks + wait [OK]
Hint: TaskGroup runs tasks together and waits for all [OK]
Common Mistakes:
Thinking TaskGroup runs only one task
Confusing TaskGroup with error handling
Assuming TaskGroup pauses tasks
2. Which of the following is the correct way to add a task inside a TaskGroup in Swift?
easy
A. group.runTask(fetchData())
B. group.createTask(fetchData)
C. group.addTask { await fetchData() }
D. group.startTask { fetchData() }
Solution
Step 1: Recall the method to add tasks
The correct method to add a task is addTask with a closure.
Step 2: Check syntax correctness
group.addTask { await fetchData() } uses group.addTask { await fetchData() }, which is the right syntax.
Final Answer:
group.addTask { await fetchData() } -> Option C
Quick Check:
Use addTask with closure [OK]
Hint: Use addTask with closure to add work [OK]
Common Mistakes:
Using wrong method names like runTask or createTask
Not using closure syntax
Omitting await inside the task
3. What will be the output of this Swift code using a TaskGroup?
func fetchNumber(_ n: Int) async -> Int {
return n * 2
}
func runGroup() async {
await withTaskGroup(of: Int.self) { group in
for i in 1...3 {
group.addTask { await fetchNumber(i) }
}
var results = [Int]()
for await result in group {
results.append(result)
}
print(results.sorted())
}
}
Task {
await runGroup()
}
medium
A. Compilation error
B. [1, 2, 3]
C. [6, 4, 2]
D. [2, 4, 6]
Solution
Step 1: Understand the task group adds tasks for numbers 1 to 3
Each task returns the input number multiplied by 2: 2, 4, and 6.
Step 2: Collect results and sort them before printing
Results are collected in any order but sorted before printing, so output is [2, 4, 6].
Final Answer:
[2, 4, 6] -> Option D
Quick Check:
Results doubled and sorted = [2,4,6] [OK]
Hint: Remember to sort results after parallel tasks [OK]
Common Mistakes:
Assuming results print in order added
Forgetting to await inside addTask
Confusing output with original numbers
4. Identify the error in this Swift code using a TaskGroup:
await withTaskGroup(of: Int.self) { group in
for i in 1...3 {
group.addTask { fetchNumber(i) }
}
for await result in group {
print(result)
}
}
Assuming fetchNumber is an async function.
medium
A. Missing await inside addTask closure
B. Cannot use for await with TaskGroup
C. TaskGroup must be declared with var
D. fetchNumber must return Void
Solution
Step 1: Check how fetchNumber is called inside addTask
fetchNumber is async, so it must be awaited inside the closure.
Step 2: Identify missing await keyword
The closure calls fetchNumber(i) without await, causing a compile error.
5. You want to fetch data from multiple URLs in parallel using a TaskGroup and collect only the successful results (non-nil). Which approach correctly filters out nil results while running tasks in parallel?
hard
A. Run tasks sequentially inside the group to avoid nil results
B. Add tasks returning optional data, then filter non-nil in for await loop
C. Use addTask with throwing function and catch errors outside group
D. Use withThrowingTaskGroup but ignore errors and collect all results
Solution
Step 1: Understand the goal to run tasks in parallel and filter nil
Running tasks in parallel with TaskGroup allows concurrency; filtering nil after collecting results is straightforward.
Step 2: Evaluate options for filtering nil results
Add tasks returning optional data, then filter non-nil in for await loop correctly adds tasks returning optional data and filters non-nil results in the for await loop, matching the goal.
Final Answer:
Add tasks returning optional data, then filter non-nil in for await loop -> Option B
Quick Check:
Filter nil after parallel tasks with for await [OK]
Hint: Filter nil results after collecting parallel task outputs [OK]
Common Mistakes:
Trying to run tasks sequentially losing parallelism
Ignoring errors without filtering nil
Misusing throwing task groups for simple filtering
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.