Set your Publisher ID in src/config/ads.ts to display live ads.
Modern Concurrency: Async / Await & Actors
Structured concurrency, Task groups, and eliminating data races with actors
Swift 5.5 introduced structured concurrency with async/await and actor types that provide compiler-enforced data race safety.
Async/Await and Actors
async: Marks functions that perform asynchronous non-blocking work.
await: Pauses execution until the asynchronous task yields a result.
actor: A reference type like a class, but with thread-safe isolation. Only one task can access an actor's mutable state at any given moment, making data races physically impossible!
@MainActor: Guarantees code runs strictly on the main thread for UI updates.
// Actors protect internal mutable state from concurrent race conditions
actor BankAccount {
var balance: Double = 1000.0
func withdraw(amount: Double) -> Boolean {
guard balance >= amount else { return false }
balance -= amount
return true
}
}
// Accessing actor methods requires await!
let account = BankAccount()
Task {
let success = await account.withdraw(amount: 250.0)
print("Withdrawal success: \(success)")
}Set your Publisher ID in src/config/ads.ts to display live ads.
Test Yourself With Exercise
What unique safety feature does an `actor` provide over a standard `class` in Swift?