Challenge - 5 Problems
Async/Await Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate2:00remaining
Understanding async/await benefits
Which of the following best explains why async/await makes concurrent code easier to write and read in Swift?
Attempts:
2 left
💡 Hint
Think about how async/await changes the way you write asynchronous code compared to completion handlers.
✗ Incorrect
Async/await lets you write asynchronous code in a style that looks like normal, linear code. This avoids deeply nested callbacks and makes the code easier to understand and maintain.
❓ ui_behavior
intermediate2:00remaining
Async function UI update behavior
Consider this Swift async function that fetches data and updates a label. What happens to the UI while the data is loading?
iOS Swift
func loadData() async { let data = await fetchData() DispatchQueue.main.async { label.text = data } }
Attempts:
2 left
💡 Hint
Async functions suspend without blocking the main thread.
✗ Incorrect
Using async/await suspends the function without blocking the main thread, so the UI stays responsive. After data loads, the label updates on the main thread.
❓ lifecycle
advanced2:00remaining
Async/await and task cancellation
What happens if a Swift async task is cancelled while awaiting a long-running operation?
iOS Swift
Task {
try Task.checkCancellation()
let result = await longRunningOperation()
print(result)
}Attempts:
2 left
💡 Hint
Check how Swift handles cancellation in async tasks.
✗ Incorrect
When a task is cancelled, calling Task.checkCancellation() throws a CancellationError, stopping the task's execution cleanly.
advanced
2:00remaining
Async/await impact on navigation flow
In a SwiftUI app, how does using async/await inside a button action affect navigation transitions?
iOS Swift
Button("Load") { Task { await loadData() navigationPath.append("DetailView") } }
Attempts:
2 left
💡 Hint
Async/await lets you wait for data before changing navigation state.
✗ Incorrect
Using await inside Task suspends until loadData() finishes, so navigation happens after data is ready, improving user experience.
🔧 Debug
expert2:00remaining
Diagnosing async/await deadlock
What is the cause of this Swift code deadlocking the UI?
iOS Swift
func fetchData() async -> String { // Simulate network delay try? await Task.sleep(nanoseconds: 1_000_000_000) return "Data" } func load() { let data = try! await fetchData() print(data) } // Called from main thread load()
Attempts:
2 left
💡 Hint
Check how async functions must be called from async contexts.
✗ Incorrect
Awaiting an async function directly inside a synchronous function blocks the main thread, causing a deadlock because the async code cannot resume.