Skip to content

ahmetbostanciklioglu/Actor_in_SwiftUI

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 

Repository files navigation

🎭 Swift Basics: Actor

A concise, runnable SwiftUI example demonstrating how actor prevents data races in concurrent Swift code.

Platform iOS Swift SwiftUI Xcode Stars Last commit

📖 Overview

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.

📚 What it covers

  • Declaring an actor (DataManager) with private, actor-isolated mutable state.
  • Calling an async method on an actor from SwiftUI using await.
  • Wrapping asynchronous work in a Task { ... } inside a button action.
  • Using Task.sleep to simulate a delayed asynchronous operation.
  • Driving the UI with @State and reflecting async results back into a Text view.

📸 Preview

Actor in SwiftUI - initial state

Actor in SwiftUI - after loading data

🚀 Getting Started

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.

📋 Requirements

  • Xcode 16.2 or later
  • iOS 18.2+ deployment target
  • Swift 5.0

🧑‍💻 Author

Ahmet Bostancıklıoğlu@ahmetbostanciklioglu · ahmetbostancikli@gmail.com

⭐ If this helped you, consider giving the repo a star!

About

A concise, runnable SwiftUI example showing how Swift's actor type prevents data races in concurrent code.

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages