I'm having a bit of a hard time with Swift's Concurrency model

It feels like with keywords like nonisolated, and swift's runtime, means concurrency is more opt-out than it is opt-in, and this is may be where it's tripping me up.

But I run into errors like this:

```
error: call to main actor-isolated static method 'receive(from:onMessage:)' in a synchronous nonisolated context
```

And I sort of guess my way around throwing keywords at the code until the compiler is happy, rather than intuitively understanding at this point what is going on.

But this makes me feel uncomfortable. I don't really understand how my code is interacting with the hardware.

Can anyone shed some light in a few words, or point me to a small blog, or recommend a small tutorial, talk, whatever on Swift's concurrency model.

Note that I don't need to be taught what concurrency is, when to use it etc, but I am naive when it comes to how its used in Swift, and SwiftUI.

2 Likes

As a starting point, I would direct you towards the concurrency chapter in The Swift Programming Language book. It’s a good general introduction. Another useful resource is the Migrating to Swift 6 guide.

P.S. If you’re interested in the Swift Evolution proposals that introduced all these features, I keep a list of the relevant proposals here: GitHub - NotTheNHK/Swift-Concurrency-Resources: A collection of Swift related concurrency resources. · GitHub

3 Likes

you should probably turn off the default main actor isolation which is on for some reason for most new Xcode projects and packages, despite the problems it causes

4 Likes

Like Keith mentioned above, I'd highly recommend switching the "Main Actor Isolation" option off as the very first step – whether you're a beginner or not.

Then, manual concurrency management is (surprisingly) unnecessary for a large number of tasks. For example, if you use URLSession for networking instead of old-style synchronous socket calls, you can do pretty much everything on the main thread: starting the network request, receiving the completion on the main thread, and updating your SwiftUI model state from there. The actual networking happens asynchronously on a system thread, mostly invisible to the caller. That said, there will still be times when older-style APIs need a bit of "assurance," like in the example below.

@MainActor class SomeClass {
    let session = URLSession(configuration: .default, delegate: nil, delegateQueue: .main)
    
    func updateModelState(data: Data?, response: URLResponse?, error: Error?) {
        // ...
    }

    func sendRequest(_ request: URLRequest) {
        // main thread
        let task = session.dataTask(with: request) { data, response, error in
            // we are on main thread here (still) thanks to the delegate queue set to `main` above
            // just this API does not know about it
            self.updateModelState(data: data, response: response, error: error)
            // ⚠️ Warning: Call to main actor-isolated global function 'updateModelState(data:response:error:)' in a synchronous nonisolated context
            MainActor.assumeIsolated {
                self.updateModelState(data: data, response: response, error: error) // ✅
            }
        }
        task.resume()
    }
}
1 Like

In addition to @NotTheNHK's useful link suggestions, I came across this AI agent skill file that possibly qualifies as a small tutorial: Swift-Concurrency-Agent-Skill/skills/swift-concurrency/SKILL.md at main · AvdLee/Swift-Concurrency-Agent-Skill · GitHub

I am just coming back to this after a hiatus working on other things for a couple of years, so the term Approachable Swift Concurrency was new to me until this afternoon.

Onward and upward.

Thanks everyone for the really cool advice!

1 Like

That matches my experience too. Turning off the default main actor isolation made the model much easier to follow, and keeping network work in small async helpers kept the rest of the code simple.

I was able to adopt the new default mode by identifying parts of my project that were marked nonisolated, specifically SwiftUI path drawing and some view modifiers. in Swift 6 mode it went from several hundred errors to zero once I’d grasped the need to group the geometry operations correctly. Hopefully that might help someone else in a similar situation.