Consider this SwiftUI view using an async function to fetch data:
struct ContentView: View {
@State private var message = "Loading..."
var body: some View {
Text(message)
.task {
message = await fetchMessage()
}
}
func fetchMessage() async -> String {
try? await Task.sleep(nanoseconds: 2_000_000_000)
return "Hello, async!"
}
}What text will be shown on screen after 2 seconds?
struct ContentView: View {
@State private var message = "Loading..."
var body: some View {
Text(message)
.task {
message = await fetchMessage()
}
}
func fetchMessage() async -> String {
try? await Task.sleep(nanoseconds: 2_000_000_000)
return "Hello, async!"
}
}Think about what the .task modifier does and how await pauses the function.
The .task modifier runs the async code when the view appears. The await fetchMessage() waits 2 seconds, then updates message. This triggers the view to show "Hello, async!".
Given this code snippet:
func loadData() async {
print("Start loading")
try? await Task.sleep(nanoseconds: 1_000_000_000)
print("Finished loading")
}
func example() {
loadData()
print("After loadData call")
}What is the order of printed lines when example() is called?
func loadData() async { print("Start loading") try? await Task.sleep(nanoseconds: 1_000_000_000) print("Finished loading") } func example() { loadData() print("After loadData call") }
Remember that calling an async function without await starts it but does not wait for it to finish.
Calling loadData() without await starts the async function but does not pause example(). So "Start loading" prints first, then "After loadData call" immediately, and finally "Finished loading" after 1 second.
Choose the correct Swift function definition that waits 1 second and returns 42 asynchronously.
In Swift, the async keyword goes after the function parentheses.
The correct syntax is func name() async -> ReturnType. Options A, B, and C misuse the placement of async, causing syntax errors.
Analyze this code snippet:
func fetchData() async -> String {
DispatchQueue.global().async {
print("Fetching data")
}
return "Done"
}When calling await fetchData(), why might this cause unexpected behavior?
func fetchData() async -> String { DispatchQueue.global().async { print("Fetching data") } return "Done" }
Think about how DispatchQueue.global().async works compared to Swift concurrency.
The DispatchQueue.global().async runs code asynchronously but does not suspend the async function. So fetchData() returns "Done" immediately, while the print happens later. This can cause race conditions if the caller expects the work to be done.
Choose the best explanation for why Swift introduced async/await instead of using completion handlers for asynchronous code.
Think about how code looks and feels when using async/await compared to nested callbacks.
Async/await allows writing asynchronous code in a linear, easy-to-read style, avoiding deeply nested completion handlers (callback hell). It does not force main thread execution, remove error handling, or automatically parallelize tasks.