Actor
A Swift type that protects its mutable state by running only one of its methods at a time.
Definition
An actor is like a class whose state can only be touched from inside the actor. Code outside must await to call it, and the actor handles those calls one at a time, so two tasks can never change its data at the same moment. The main actor is a special global actor for UI code. Actors are reentrant: while a method is suspended at an await, other calls can run, so state can change across an await.
In practice
actor ThumbnailCache { private var thumbnails: [URL: Data] = [:] } is safe to use from many parallel download tasks, each calling await cache.store(data, for: url).
Learn it in context
Lessons that use this term.
Related terms
- SendableA Swift protocol that marks values as safe to pass between concurrently running tasks and actors.
- Data raceTwo threads accessing the same memory at the same time, at least one of them writing, without synchronization.
- async/awaitWriting asynchronous code that reads top to bottom like normal code, with await marking where it may pause.