Skip to content
Commitwise
Menu

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

Esc

Type to search the whole site.