LESSON 6 OF 6

Concurrency and actors

Understand suspension, isolation, and shared state.

Await is a suspension point

async permits a function to suspend, and await marks where that can happen. It does not guarantee that work runs on a background thread. Keep expensive synchronous work away from UI isolation when it would block responsiveness.

Isolate mutable state

An actor protects its isolated state. Calling its methods from outside the isolation domain generally requires await. Sendable expresses that a value can safely cross concurrency boundaries under the language rules.

Recheck assumptions after await

An actor can do other work while an async method is suspended. Data may have changed by the time execution resumes. Plan multi-step operations around that reentrancy instead of assuming a single uninterrupted transaction.

Try it yourself

main.swift
actor Counter {
    private var value = 0
    func increment() { value += 1 }
    func current() -> Int { value }
}
let counter = Counter()
await counter.increment()
print(await counter.current())

Actors prevent many data races, but do not automatically make multi-step operations atomic.

Continue with the official Swift language guide