A simple SwiftUI app that demonstrates how and when to use @MainActor with @Observable in Swift 6.
- Built with Swift 6 and SwiftUI
- Uses
@Observablewith@MainActor - Demonstrates proper state handling without
@StateObject - Includes both a safe and an unsafe version to illustrate concurrency errors
MainActorCounter/
βββ Models/
β βββ CounterModel.swift
βββ Views/
β βββ CounterView.swift
βββ MainActorCounterApp.swift
- Open
MainActorCounter.xcodeprojin Xcode - Choose a simulator
- Run the app
- Tap the increment button and watch the state update safely on the main thread
β οΈ Note: If you're using a free Apple Developer account, you may hit the 3-app install limit on physical devices.
Check out the full explanation here: π Read on Medium