0
0
iOS Swiftmobile~20 mins

Structured concurrency in iOS Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Structured Concurrency Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2: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()
}
AResults: Second, First
BResults: First, Second
CResults: nil, nil
DCompilation error due to missing await
Attempts:
2 left
💡 Hint
Remember that async let starts tasks concurrently and await waits for their results.
lifecycle
intermediate
2: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()
}
APrints "Parent cancelling child" then "Parent finished"; "Child finished" is never printed
BPrints "Child finished" then "Parent cancelling child" then "Parent finished"
CPrints "Parent cancelling child" then "Child finished" then "Parent finished"
DCompilation error due to incorrect Task usage
Attempts:
2 left
💡 Hint
Consider what happens when a Task is cancelled before it finishes sleeping.
🧠 Conceptual
advanced
2:00remaining
Why use TaskGroup in Swift concurrency?
Which statement best explains the main advantage of using TaskGroup in Swift structured concurrency?
AIt automatically retries failed tasks without developer intervention.
BIt serializes tasks to run one after another to save memory.
CIt allows running multiple child tasks concurrently and waits for all to complete, collecting their results safely.
DIt replaces async let for single asynchronous calls.
Attempts:
2 left
💡 Hint
Think about how TaskGroup manages multiple tasks and their results.
📝 Syntax
advanced
2: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" }
AMissing await before data1 in print statement
Basync let cannot be used with fetchData1()
CfetchData2() must be called without await
Dprint statement must be inside a Task block
Attempts:
2 left
💡 Hint
Remember how to get the value from an async let binding.
🔧 Debug
expert
2: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()
}
AThe print statement is unreachable due to syntax error
BTask cannot be created inside an async function
CMissing await before task creation causes deadlock
DThe task recursively awaits itself causing infinite recursion and deadlock
Attempts:
2 left
💡 Hint
Look at the recursive call inside the Task and what it awaits.