Sanniv Khare 6 min read

Default MainActor isolation in Swift 6.2: porting a package

Default MainActor isolation in Swift 6.2: porting a package

Turning on .defaultIsolation(MainActor.self) in an existing Swift package changes two things at once: every unannotated type and function becomes @MainActor, and with Xcode’s “Approachable Concurrency” features a plain nonisolated async function stops leaving the main actor. The takeaway: mark value types and pure helpers that other actors use as nonisolated, and mark heavy async work @concurrent, before you flip the setting.

We built the same file four ways on our M1 lab Mac with Xcode 26.2 (17C52) and Swift 6.2.3 (macOS 15.7.3 host). The Swift 6 defaults build passed. Default MainActor isolation failed with two errors. Adding nonisolated in two places made it build again.

Why we did this: new app targets in Xcode 26 start with MainActor isolation by default, while our older packages (the shared Swift code behind Nix and OneClick QuitAll on the Mac, and the native modules in our iOS apps) do not. Moving a package over is the work in front of us, and the compiler errors are not obvious the first time you see them.

The settings we compared

Four copies of one small package, each built with swift build --target Thumbs:

  • A, no settings (Swift 6 language mode): builds.
  • B, .defaultIsolation(MainActor.self): 2 errors.
  • C, B plus NonisolatedNonsendingByDefault and InferIsolatedConformances: the same 2 errors.
  • D, C plus two nonisolated keywords in the source: builds.

The C settings are what Xcode’s “Approachable Concurrency” build setting turns on in an app target. In a package you spell them out:

// swift-tools-version: 6.2
import PackageDescription

let approachable: [SwiftSetting] = [
    .defaultIsolation(MainActor.self),
    .enableUpcomingFeature("NonisolatedNonsendingByDefault"),
    .enableUpcomingFeature("InferIsolatedConformances"),
]

let package = Package(
    name: "Thumbs",
    platforms: [.macOS(.v15), .iOS(.v18)],
    targets: [
        .target(name: "Thumbs", swiftSettings: approachable),
    ]
)

The proposals behind each line: SE-0466 (default isolation), SE-0461 (nonisolated async functions run on the caller’s actor), SE-0470 (isolated conformances). All three are marked implemented in Swift 6.2.

The source that broke

This is ordinary pre-6.2 code: a cache, a Codable value type, an actor that encodes it, and a free function used from a detached task.

import Foundation

final class ImageCache {
    private var store: [String: Data] = [:]
    func data(for key: String) -> Data? { store[key] }
    func insert(_ data: Data, for key: String) { store[key] = data }
}

struct Thumbnail: Codable, Sendable {
    var key: String
    var bytes: Int
}

actor Downloader {
    func fetch(_ key: String) async -> Thumbnail {
        Thumbnail(key: key, bytes: key.count * 1024)
    }
    func encode(_ t: Thumbnail) throws -> Data {
        try JSONEncoder().encode(t)
    }
}

func checksum(_ data: Data) -> Int {
    data.reduce(0) { $0 &+ Int($1) }
}

enum Work {
    static func sum(_ items: [Data]) async -> Int {
        await Task.detached {
            items.map(checksum).reduce(0, +)
        }.value
    }
}

With MainActor as the default, Thumbnail, checksum and ImageCache all became @MainActor. The compiler then rejected the two places where other isolation domains touch them:

Compiler output: two errors after turning on MainActor default isolation

Real output from our M1 lab Mac, paths shortened. Variants B and C printed the same two errors.

Error 1: the Codable conformance is now main-actor isolated

main actor-isolated conformance of 'Thumbnail' to 'Encodable' cannot be used in actor-isolated context. The struct is @MainActor, so its synthesized Encodable conformance is too. Downloader runs on its own actor, and using a main-actor conformance there would need a hop the generic encode call cannot make.

Variant B shows this error even without the InferIsolatedConformances flag. SE-0470 says a module that infers @MainActor should also have isolated conformance inference on; with Swift 6.2.3 in our build, the conformance was isolated either way.

Notice what did not fail: Thumbnail(key:bytes:) inside the actor compiled. The error is about the protocol conformance, not about creating the value.

Error 2: a MainActor function passed as a plain closure

converting function value of type '@MainActor (Data) -> Int' to '(Data) -> Int' loses global actor 'MainActor'. items.map(checksum) inside Task.detached needs a nonisolated function, and checksum is now main-actor isolated.

The fix: say which code is not UI code

Both errors have the same answer: tell the compiler the type and the helper belong to no actor. nonisolated on a type declaration (SE-0449, Swift 6.1) stops global actor inference for the type and its members, including the synthesized conformances.

Before and after: nonisolated on the struct and the helper function

Variant D: the only source change. It built cleanly.

ImageCache stays @MainActor, which is what you want for a cache the UI reads. The rule of thumb we are using in the port:

  • Value types that cross actors (models, DTOs, anything Codable that a networking or database actor encodes): nonisolated struct.
  • Pure helpers called from tasks or other actors: nonisolated func.
  • Classes that hold UI state: leave them alone; MainActor is the point.
  • Actors: unchanged. An explicit actor keeps its own isolation.

The quiet change: where nonisolated async functions run

The compile errors are the loud half. NonisolatedNonsendingByDefault changes runtime behaviour without any error. Under Swift 6 defaults, a nonisolated async function always ran on the global concurrent executor. With the feature on, it runs on the caller’s actor, so a call from the main actor stays on the main thread. To get the old behaviour you write @concurrent.

We checked this with a small executable that asks pthread_main_np() from inside each function:

import Darwin

nonisolated func onMainThread() -> Bool { pthread_main_np() == 1 }

nonisolated func work() async -> Bool { onMainThread() }

@concurrent nonisolated func workConcurrent() async -> Bool { onMainThread() }

@MainActor func report() async {
    print("caller on main thread:", onMainThread())
    print("nonisolated async func on main thread:", await work())
    print("@concurrent func on main thread:", await workConcurrent())
}

await report()
Probe output under both settings

Release builds on our M1 lab Mac, one run of each. Under Swift 6 defaults the nonisolated function ran off the main thread; with the C settings it ran on it.

If an older package relied on nonisolated async to move parsing, image decoding or file hashing off the main thread, it will keep compiling and start doing that work on the main thread. Search for nonisolated functions that are async and do heavy work, and mark them @concurrent.

Porting order we follow

  1. Build the package once with the Swift 6 defaults and fix every warning first.
  2. Add .defaultIsolation(MainActor.self) to one target at a time, starting with the target closest to the UI.
  3. For each conformance error, mark the type nonisolated if it is a value type other actors use; otherwise move the call onto the main actor.
  4. For each “loses global actor” error, mark the helper nonisolated.
  5. Turn on NonisolatedNonsendingByDefault, then grep for nonisolated async functions and decide, one by one, whether they need @concurrent.
  6. Run your tests. The runtime change in step 5 shows up only there.

What to check on your own app

  • Shared model packages are the risky ones: a model target set to MainActor by default makes every model main-actor isolated for every consumer. We keep model and networking targets on the Swift 6 default and switch only UI targets.
  • Macro-generated conformances (@Observable, Codable synthesis) follow the type’s isolation, as the first error shows.
  • Treat the package above as a starting point: copy it, build it against your toolchain, and compare the diagnostics with your own code before changing build settings across a whole project.

The steps above were built with Xcode 26.2 and Swift 6.2.3 on macOS 15.7.3; later compilers may word the diagnostics differently.

Made by the team behind this post

Apps from our studio

  • Goes with this post

    Cardigami

    Pop-up greeting cards that rise out of the fold on iPhone Duo.

    iPhone · App Store

    Get
  • Goes with this post

    Nix

    The Mac uninstaller that finds what the Trash leaves behind: caches, logs, derived data and saved states.

    macOS

    Get
  • Goes with this post

    OneClick QuitAll

    Quit every running app in one click. See memory and CPU first.

    macOS · Mac App Store

    Get
  • BMDB.fun

    Your Bollywood world at a glance, with links straight to the OTT app that streams each film.

    Android TV · Web · Mobile

    Get

Need this built? Let’s talk.

We build and run apps for Mac, TV, mobile and the web, and write about what we learn.