Challenge - 5 Problems
Structured Concurrency Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
Understanding async let behavior in Swift concurrency
What will be printed when this Swift code runs?
iOS Swift
func fetchData() async { async let first = fetchFirst() async let second = fetchSecond() let result1 = await first let result2 = await second print("Results: \(result1), \(result2)") } func fetchFirst() async -> String { return "First" } func fetchSecond() async -> String { return "Second" } Task { await fetchData() }
Attempts:
2 left
💡 Hint
Remember that async let starts tasks concurrently and await waits for their results.
✗ Incorrect
The async let declarations start both fetchFirst() and fetchSecond() concurrently. Awaiting first and second gets their results in the order awaited, so the print shows "First" then "Second".
❓ lifecycle
intermediate2:00remaining
Task cancellation in structured concurrency
What happens when the child task is cancelled before it completes in this Swift code?
iOS Swift
func parentTask() async throws { let child = Task { try await Task.sleep(nanoseconds: 2_000_000_000) print("Child finished") } try await Task.sleep(nanoseconds: 1_000_000_000) print("Parent cancelling child") child.cancel() print("Parent finished") } Task { try await parentTask() }
Attempts:
2 left
💡 Hint
Consider what happens when a Task is cancelled before it finishes sleeping.
✗ Incorrect
The child task is cancelled before its sleep finishes, so it never prints "Child finished". The parent prints its messages and finishes normally.
🧠 Conceptual
advanced2:00remaining
Why use TaskGroup in Swift concurrency?
Which statement best explains the main advantage of using TaskGroup in Swift structured concurrency?
Attempts:
2 left
💡 Hint
Think about how TaskGroup manages multiple tasks and their results.
✗ Incorrect
TaskGroup lets you create many child tasks that run concurrently and then await all their results safely, making it easier to manage dynamic numbers of tasks.
📝 Syntax
advanced2:00remaining
Identify the syntax error in this Swift concurrency code
Which option correctly identifies the syntax error in the following Swift code snippet?
iOS Swift
func loadData() async { async let data1 = fetchData1() let data2 = await fetchData2() print(await data1, data2) } func fetchData1() async -> String { "Data1" } func fetchData2() async -> String { "Data2" }
Attempts:
2 left
💡 Hint
Remember how to get the value from an async let binding.
✗ Incorrect
async let creates a concurrent task and returns a value that must be awaited before use. The print statement uses data1 without await, causing a compile error.
🔧 Debug
expert2:00remaining
Diagnose the cause of a deadlock in Swift structured concurrency
Given this Swift code, why does the program deadlock and never print "Done"?
iOS Swift
func deadlockExample() async { let task = Task { await deadlockExample() } await task.value print("Done") } Task { await deadlockExample() }
Attempts:
2 left
💡 Hint
Look at the recursive call inside the Task and what it awaits.
✗ Incorrect
The Task runs deadlockExample() which creates another Task that awaits deadlockExample() again, causing infinite recursion and deadlock before printing "Done".