0
0
iOS Swiftmobile~5 mins

Task and TaskGroup in iOS Swift - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a Task in Swift concurrency?
A Task is a unit of asynchronous work that runs concurrently. It allows you to perform work without blocking the main thread, like fetching data or doing calculations in the background.
Click to reveal answer
intermediate
What does a TaskGroup do in Swift?
A TaskGroup lets you run multiple tasks in parallel and wait for all of them to finish. It helps manage a group of related asynchronous tasks together.
Click to reveal answer
beginner
How do you create a Task in Swift?
You create a Task by calling Task { } and putting your asynchronous code inside the closure. For example: Task { await fetchData() }
Click to reveal answer
intermediate
Why use TaskGroup instead of multiple separate Tasks?
TaskGroup helps you keep track of all tasks as a group, making it easier to wait for all to finish and handle their results together. It also manages cancellation and errors more cleanly.
Click to reveal answer
advanced
What happens if one Task in a TaskGroup throws an error?
If a Task in a TaskGroup throws an error, the whole TaskGroup throws that error and cancels remaining tasks. This helps you handle failures early and avoid wasted work.
Click to reveal answer
How do you start a new asynchronous Task in Swift?
ATask { await someAsyncFunction() }
BDispatchQueue.main.async { someAsyncFunction() }
CThread.start { someAsyncFunction() }
Dasync let someAsyncFunction()
What is the main benefit of using a TaskGroup?
AAutomatically retry failed tasks
BRun tasks one after another
CRun tasks only on the main thread
DRun multiple tasks in parallel and wait for all to finish
If one task in a TaskGroup fails, what happens?
AOnly that task fails, others continue
BThe whole TaskGroup throws an error and cancels remaining tasks
CThe TaskGroup ignores the error
DThe TaskGroup retries the failed task automatically
Which keyword is used to wait for a TaskGroup to finish all tasks?
Aasync
Byield
Cawait
Ddefer
Can you add tasks dynamically inside a TaskGroup?
AYes, you can add tasks while the group is running
BNo, all tasks must be declared upfront
COnly if tasks are synchronous
DOnly if tasks are on the main thread
Explain how Task and TaskGroup help manage asynchronous work in Swift.
Think about how you would cook multiple dishes at once and wait for all to be ready.
You got /4 concepts.
    Describe what happens when a task inside a TaskGroup throws an error.
    Imagine if one dish burns, you stop cooking the rest.
    You got /3 concepts.