While working on a SwiftUI project, I noticed that my build log was full of concurrency warnings. One of them kept appearing whenever I tried to use a property from the wrong context:

Property isolated to global actor ‘MainActor’ cannot be referenced from a non-isolated context

My first reaction was to wrap the code in Task { @MainActor in ... } until the warnings disappeared. That got me past the compiler, but I still could not explain why the property needed a different context in the first place.

I decided to spend some time learning about actor isolation. The idea became much clearer once I started asking which actor owns a piece of state and where the code accessing it runs.

Thinking about isolation as islands

Before learning Swift Concurrency, I mostly thought about concurrency in terms of threads and DispatchQueue. If two parts of an app changed the same value at once, I knew there could be a race condition. What I struggled with was seeing where that risk appeared in my own code.

The island metaphor helped me. An actor owns its mutable state, like an island with a boundary around it. Code isolated to that actor can access the state directly. Code outside that boundary needs to cross into the actor’s context before it can access actor-isolated state.

MainActor is the island I encounter most often in a SwiftUI app. It is a global actor associated with the main thread, and UI state is often isolated to it. A function outside MainActor cannot simply change a @MainActor property. It needs to make that transition explicitly, often with await.

The metaphor has a limit: an actor is about isolation, not a promise that every Task runs on a separate thread. A task can inherit the actor context where it was created, so a hop to MainActor isn’t always needed.

Here are three situations that helped me understand the warnings.

1. Updating UI state from a nonisolated context

Suppose a function that is not isolated to MainActor fetches data and then needs to update a @MainActor view model. The fetch can finish outside the actor, but changing the view model’s state requires crossing its boundary.

The compiler might show a warning like this:

Property ‘state’ isolated to global actor ‘MainActor’ cannot be mutated from a non-isolated context

I can make the transition explicit with MainActor.run:

func loadData() {
    Task {
        let data = await api.fetch()

        await MainActor.run {
            self.state = .loaded(data)
        }
    }
}

This example assumes loadData() is called from a nonisolated context. If the method already belongs to a @MainActor view model, the Task can inherit that isolation and the explicit MainActor.run may be unnecessary. That distinction is what I missed when I first started adding actor hops everywhere.

2. Conforming to a nonisolated protocol

Another warning appeared when a @MainActor view model conformed to a protocol whose requirement was not actor-isolated:

MainActor-isolated instance method cannot be used to satisfy a non-isolated protocol requirement

The protocol allows its method to be called without entering MainActor, but the view model’s method is isolated to MainActor by default, so the two don’t match.

If the method does not need actor-isolated state, I can mark it nonisolated:

@MainActor
class ViewModel: MyDelegate {

    nonisolated func didFinishWork() {
        print("Work finished!")
    }
}

That works for a method like this one, which only prints a message. If the delegate method needs to update the view model, I still have to cross back to MainActor through an appropriate async path. Marking the method nonisolated does not make its isolated properties safe to access directly.

3. A callback scheduled on the main queue

This situation confused me the most. I was observing a notification and asking NotificationCenter to deliver the callback on the main queue:

NotificationCenter.default.addObserver(
    forName: .updated,
    object: nil,
    queue: .main
) { _ in
    self.updateUI()
}

I expected queue: .main to make self.updateUI() acceptable. The compiler still sees a closure whose type does not say it is isolated to MainActor. The queue choice controls where the callback is delivered at runtime; it does not give the closure a compile-time actor guarantee.

If I know the callback will run in MainActor’s context, I can state that assumption with assumeIsolated:

NotificationCenter.default.addObserver(
    forName: .updated,
    object: nil,
    queue: .main
) { _ in
    MainActor.assumeIsolated {
        self.updateUI()
    }
}

I would use this carefully. assumeIsolated checks the assumption at runtime and can trap if it is wrong. It is useful when an older callback API gives a guarantee that Swift’s type system cannot express.

Another option is to observe notifications through an asynchronous sequence:

Task {
    for await _ in NotificationCenter.default.notifications(named: .updated) {
        self.updateUI()
    }
}

If I create this task from a @MainActor context, it inherits that isolation, so the UI update can stay on the actor. I would also keep track of the task and cancel it when observation is no longer needed.

Closing

I used to see isolation warnings as obstacles to getting a clean build. Now I see them as questions about ownership: which actor owns this state, and does the code accessing it belong to that actor?

The island metaphor gives me a starting point, but I still need to check the actual context of each function and task. Depending on what I find, the fix might be an actor hop, nothing at all because the code is already on MainActor, or a different design for the protocol or callback.

Takeaway: When Swift warns me about isolation, I try to trace the state back to its actor before choosing a fix.