Async functions let your app do tasks without freezing the screen. They help your app stay smooth and fast.
Async functions in iOS Swift
func fetchData() async -> String {
// your code here
}Use async after func to mark a function as asynchronous.
Call async functions with await inside another async function.
func greet() async -> String { return "Hello!" }
func fetchNumber() async -> Int { return 42 }
func loadImage() async throws -> UIImage {
// code to load image
}This SwiftUI view shows a text label. It starts with "Loading...". Then it calls an async function that waits 1 second and returns a greeting. The text updates to show the greeting without freezing the app.
import SwiftUI struct ContentView: View { @State private var message = "Loading..." var body: some View { Text(message) .padding() .task { message = await fetchGreeting() } } func fetchGreeting() async -> String { // Simulate waiting for data try? await Task.sleep(nanoseconds: 1_000_000_000) // 1 second return "Hello from async!" } }
Always call async functions with await inside another async context.
Use Task.sleep to simulate delays in async functions.
Async functions help keep your app responsive and smooth.
Async functions let your app do work without freezing the screen.
Mark functions with async and call them with await.
Use async to load data, images, or do long tasks smoothly.
Practice
async in Swift mean?Solution
Step 1: Understand async function behavior
Async functions allow pausing and resuming work without freezing the app's main thread.Step 2: Compare options with async meaning
Only The function can pause and resume without blocking the app's main thread. correctly describes this behavior; others are unrelated or incorrect.Final Answer:
The function can pause and resume without blocking the app's main thread. -> Option DQuick Check:
Async means non-blocking pause/resume = A [OK]
- Thinking async means the function runs only once
- Confusing async with return types
- Believing async functions cannot call others
Solution
Step 1: Recall Swift async function syntax
In Swift,asynccomes after the parameter list and before the return type.Step 2: Match options to correct syntax
Only func fetchData() async throws -> String {} matches the correct syntax:func name() async throws -> Type {}.Final Answer:
func fetchData() async throws -> String {} -> Option AQuick Check:
Async keyword after () = C [OK]
- Placing async before func keyword
- Mixing async and throws order
- Putting async after return type
func fetchNumber() async -> Int {
return 42
}
Task {
let number = await fetchNumber()
print(number)
}Solution
Step 1: Understand async function return
The async functionfetchNumber()returns 42 as an Int.Step 2: Await call inside Task and print
Theawaitkeyword waits for the async function to finish, then prints 42.Final Answer:
42 -> Option AQuick Check:
Await fetch returns 42 printed = D [OK]
- Expecting nil because of async
- Thinking Task prints itself
- Assuming compilation error without await
func loadData() async -> String {
return "Data"
}
let result = loadData()
print(result)Solution
Step 1: Check async function call
Async functions must be called withawaitto get their result.Step 2: Identify missing await
The code callsloadData()withoutawait, causing a compile error.Final Answer:
Missing await keyword when calling async function -> Option BQuick Check:
Call async with await = B [OK]
- Calling async functions without await
- Thinking async functions can't return String
- Assuming print can't show async results
Solution
Step 1: Understand async calls order
BothfetchUserData()andfetchUserImage()are async and must be awaited to complete before next step.Step 2: Check which code awaits both calls
Only async func loadUser() { let data = await fetchUserData() let image = await fetchUserImage(data.id) print(image) } awaits both calls in order, ensuring data is ready before fetching image.Final Answer:
async func loadUser() { let data = await fetchUserData() let image = await fetchUserImage(data.id) print(image) } -> Option CQuick Check:
Await each async call in order = A [OK]
- Not awaiting async calls
- Calling async functions without async keyword
- Mixing sync and async calls incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
