A concise, runnable SwiftUI example demonstrating how actor prevents data races in concurrent Swift code.
An actor is a Swift concurrency type that protects its mutable state from data races: only one task can access an actor's isolated state at a time, and all access from outside the actor is asynchronous (await). This makes actors the go-to tool for safely sharing mutable state across concurrently running tasks and threads.
This repository is a small, runnable learning example. A SwiftUI screen holds a reference to an actor named DataManager. Tapping a button spawns a Task that calls an async method on the actor and updates the on-screen text with the returned value, illustrating the basic actor call site and the await boundary.
- Declaring an
actor(DataManager) with private, actor-isolated mutable state. - Calling an
asyncmethod on an actor from SwiftUI usingawait. - Wrapping asynchronous work in a
Task { ... }inside a button action. - Using
Task.sleepto simulate a delayed asynchronous operation. - Driving the UI with
@Stateand reflecting async results back into aTextview.
git clone https://github.com/ahmetbostanciklioglu/Actor_in_SwiftUI.git
cd Actor_in_SwiftUI
open "Actor in SwiftUI.xcodeproj"Then press ⌘R in Xcode to build and run on the iOS Simulator.
- Xcode 16.2 or later
- iOS 18.2+ deployment target
- Swift 5.0
Ahmet Bostancıklıoğlu — @ahmetbostanciklioglu · ahmetbostancikli@gmail.com
⭐ If this helped you, consider giving the repo a star!

