Your Actor Can't Be @Observable. A New Swift Pitch Wants to Fix That With Snapshots.
I have written the same class at least nine times. It’s a @MainActor @Observable final class whose only job is to sit next to an actor and copy its properties over, one by one, so a SwiftUI view can read them.
Yesterday a new Swift Evolution pitch showed up proposing to delete that class from my life. It’s called “Observable actors and actor snapshots”, it comes from the same author as the Observation performance pitch I wrote about recently, and it’s openly the reason that earlier pitch exists.
The error you’ve probably already hit
Try this in Xcode today:
import Observation
@Observable
actor Downloader {
var completed = 0
var total = 1_000
}
You get:
error: '@Observable' cannot be applied to actor type 'Downloader'
The reason is mechanical, not philosophical. The @Observable macro identifies each property with a key path, and a key path can’t name an actor-isolated property. So the class expansion just doesn’t apply.
And even if the macro did work, you’d hit wall number two. A SwiftUI body is synchronous. Reading an actor’s property from outside the actor takes an await. You can’t await in body. Game over.
So the most interesting state in your app, the long-running work you deliberately pushed off the main thread, is exactly the state Observation can’t describe.
The mirror class we all write
Here’s the workaround, roughly as it lives in at least two of my apps:
import SwiftUI
import Observation
actor Downloader {
private(set) var completed = 0
let total = 1_000
private var continuation: AsyncStream<Int>.Continuation?
func progressStream() -> AsyncStream<Int> {
AsyncStream { continuation in
self.continuation = continuation
}
}
func run() async {
for _ in 0..<total {
try? await Task.sleep(for: .milliseconds(5))
completed += 1
continuation?.yield(completed)
}
continuation?.finish()
}
}
@MainActor @Observable
final class DownloaderModel {
var completed = 0
var total = 1_000
func consume(_ stream: AsyncStream<Int>) async {
for await value in stream {
completed = value
}
}
}
struct DownloadView: View {
let downloader: Downloader
@State private var model = DownloaderModel()
var body: some View {
ProgressView(value: Double(model.completed), total: Double(model.total))
.task {
model.total = downloader.total // a `let` on an actor needs no await
let stream = await downloader.progressStream()
async let work: Void = downloader.run()
await model.consume(stream)
await work
}
}
}
It works. It’s also a small museum of everything that can go wrong:
- It drifts. Add a
statusproperty to the actor and forget the model? The view never shows it. Nothing warns you. - It’s chatty. A thousand writes on the actor become a thousand hops to the main actor, for a screen that can redraw maybe sixty times a second.
- It tears. If
completedandtotalarrive as two separate assignments, there’s a moment where the view can render a combination that never existed on the actor. - It’s a second source of truth. The model can be behind the actor and the view has no way to know.
The pitch lists these same four problems almost word for word, which was a little humbling to read. Apparently my “clever pattern” is the thing the language now wants to make unnecessary.
What the pitch proposes
There are two pieces, and they’re separable.
Piece one: @Observable on actors. You can put the macro on an actor (and on a distributed actor). Properties are tracked the way a class’s are, for observers running on the actor. Since key paths are off the table, the expansion identifies each property by its index, which is exactly the mechanism the earlier performance pitch introduced. That’s the connection between the two proposals.
Piece two: SnapshotSource. This is the part SwiftUI developers actually care about. Opt in, and the macro synthesizes a Snapshot struct mirroring your observed properties, plus a way to read it without await:
// Proposed syntax — this does NOT compile in any shipping Swift yet.
@Observable
actor Downloader: SnapshotSource {
var status = "idle"
var completed = 0
var total = 1_000
func run() async { /* writes `completed` as it goes */ }
}
struct DownloadView: View {
let downloader: Downloader
var body: some View {
let snapshot = downloader.latestSnapshot // synchronous, tracked
ProgressView(value: Double(snapshot.completed), total: Double(snapshot.total))
Text(snapshot.status)
}
}
latestSnapshot is nonisolated, so the main actor can read it directly. It’s a plain Sendable value. Reading a property of it inside body registers an Observation access, exactly like reading a property on a @Observable class. When the actor publishes a new snapshot, the view invalidates.
If you need a value that’s current right now rather than “most recently published,” there’s await downloader.snapshot, which refreshes first.
That’s the whole mirror class, gone. The compiler writes the struct, so it can’t drift from the actor.
The consistency model is the actual design
The headline feature is easy. The interesting part is that the pitch spells out precisely what “eventually consistent” means here, which is more than most of our hand-rolled mirrors ever did.
Think of it like a scoreboard operator at a basketball game. The game (the actor) changes constantly. The operator doesn’t flip the board for every dribble. They update it when there’s a pause, all at once, and the board never goes backward.
The pitch’s rules, roughly:
- Convergence. After a write, the snapshot catches up once the refresh runs, which is no earlier than the writing job’s next suspension point.
- No premature notifications. An observer is never told about a change before the snapshot containing it is published. So when your view re-renders, it reads the new value, not the old one.
- Monotonic reads. You see the current snapshot or a newer one. Never an older one.
- Coalescing. A burst of writes becomes one refresh with the final values. Intermediate values may never be visible. For a progress bar, that’s a feature.
- Consistent contents. Every value in one snapshot comes from a single job on the actor. No more
completedfrom one moment andtotalfrom another.
The pitch’s own example: a download that writes completed a thousand times, suspending every hundred, drives a progress bar through eleven renders, each a real state. A label that only reads status re-renders only when status changes.
Compare that to my mirror class: a thousand main-actor hops and no tearing guarantee. Yeah.
One subtlety worth tattooing somewhere: rule 5 applies per snapshot, not per read. downloader.latestSnapshot.completed and then downloader.latestSnapshot.total are two reads that can straddle a refresh. Pull it into a local let snapshot first, like the example does, and you’re safe.
What’s still open
It’s a pitch, posted December 2nd. The thread is already busy, and a few things are clearly unsettled.
Distributed actors. The pitch says SnapshotSource on a distributed actor is diagnosed, because there’s nowhere to store latestSnapshot on a remote reference. Swift’s distributed actors maintainer pushed back in the thread, suggesting actor-system implementations could provide the snapshot storage themselves, and another reply proposed making snapshot a typed-throwing getter so local and remote sources share a protocol. Expect this corner to move.
Lifetime. An active observation probably keeps the actor alive, same as with classes. That’s fine until it isn’t. If you’ve ever chased a retain cycle in an @Observable model, you know where my mind went.
Cost. Reading latestSnapshot copies the struct, which means a retain per reference-typed property. Put a giant array in your actor’s observed state and you’ll want @ObservationIgnored on it, which the pitch says removes the property from both observation and the snapshot.
What I’d do today
Nothing ships until review, acceptance, and a toolchain release, so for now the mirror class lives. But you can write it so the migration is cheap later:
- Publish one struct, not N properties. Have the actor yield a
Sendablestate struct through its stream, and assign it to the model in one go. That kills tearing today and maps one-to-one ontoSnapshottomorrow. - Throttle on the actor side. Yield at suspension points or every N writes, not every write. Your main actor will thank you.
- Keep the actor the source of truth. The model should never compute anything of its own. If it does, that logic has nowhere to go when the mirror disappears.
And remember that an actor isn’t a lock: every await in run() is a spot where another call can slip in and change state your snapshot will happily publish. Snapshots make reading actor state easier. They don’t make reentrancy go away.
If the isolation side of this is still fuzzy, the Swift 6 concurrency lesson in SwiftUI in Practice walks through actors and @MainActor with a real app, and my @MainActor vs nonisolated decision tree covers why latestSnapshot has to be nonisolated to be useful at all.
I’ll be watching this thread. Any proposal that deletes a class I’ve copy-pasted nine times has my full attention.
Share this post
Comments
Leave a comment
NativeFirst Team
EditorialThe NativeFirst team — engineers and designers building native Apple apps and writing the courses we wish we had when we started.