Understanding Swift 6.2 Concurrency: MainActor, nonisolated and @concurrent

Hi everyone :waving_hand:t3:

First, thanks to Konrad for the initial explanation and for suggesting that I move this discussion to the public forums so others can benefit from it as well. His explanation helped me understand that these changes are mainly about controlling where async code executes and why that matters for runtime performance, even when using AI coding agents.

I'm relatively new to Swift and currently learning more about Swift concurrency. I've been exploring two proposals introduced in Swift 6.2,

  • SE-0466: Control default actor isolation inference.

  • SE-0461: nonisolated(nonsending) and @concurrent.

I understand the basic concepts, but I'm trying to understand how they work together in real applications.

1. How do default actor isolation and async execution work together?

SE-0466 allows a project or module to configure MainActor as its default isolation.

For example,

@MainActor
class UserViewModel {
    var username = ""

    func updateUsername(_ name: String) {
        username = name
    }
}

If a project uses MainActor as its default isolation, how should we handle functions that perform background work? Does explicitly using nonisolated provide a way to separate that work from MainActor, and how does this interact with async functions?

2. When should we use nonisolated(nonsending) and @concurrent?

SE-0461 introduces different ways to control where nonisolated async functions execute.

nonisolated(nonsending)
func fetchUserData() async {
    // Async work using the caller's actor
}

@concurrent
func processUserData() async {
    // Work on the generic executor
}

Imagine a SwiftUI application that fetches data from an API, processes a large amount of data, and then updates the UI... Which execution model would be appropriate for each operation? Would @concurrent be the right choice for CPU-intensive processing, and when would a custom actor be more appropriate?

I'd also like to understand how non Sendable values are handled when passing data between these isolation contexts.

3. How do actors, executors and threads relate to each other?

This is probably one of the areas I'm finding most difficult to understand....

I understand that actors provide isolation and executors determine where jobs are scheduled, but I'm still trying to connect these concepts.

For example does switching to the generic executor necessarily mean switching to another thread? How does Swift's cooperative thread pool schedule concurrent tasks, particularly when some tasks perform CPU intensive work?

4. Learning Swift in the AI era

One final question, following my earlier conversation with Konrad.

Konrad mentioned that even with AI agents, it's still important to understand what's happening inside a system rather than relying entirely on generated code. I found that perspective interesting, especially as someone who is still relatively new to Swift.

With AI coding agents becoming increasingly capable of generating Swift code, how deeply should beginners understand concepts like concurrency, memory management and system behaviour?Would you recommend learning these fundamentals in depth before building larger applications, or focusing on building projects first and learning the deeper concepts along the way?

I'd be interested to hear how experienced Swift developers approach this, particularly in today's AI assisted development environment.

I'm still learning, so please feel free to correct anything I've misunderstood.

Thanks again to @ktoso for the initial guidance and thanks to everyone who takes the time to share their knowledge :)

Hi mate. I'm still mastering these ideas myself, so I can't really offer much sage advice. But I think it's fair to say that Swift is a treat for computer scientists and CS enthusiasts, so regardless of AI it's worth studying the details if that's something you're interested in.

Looking forward to seeing what others have to say here, I'm still in Grand Central Dispatch hoping to get better with Concurrency.

2 Likes

A beginner will never improve if they let AI handle all the non-beginner stuff. How could they review what the AI has created if they aren't yet qualified to be a code reviewer?

4 Likes

Hey mate :waving_hand:t3: Thanks for sharing... I agree, Swift has so much to explore and I think that's one of the things that makes learning it interesting. GCD is a good starting point too :100:

1 Like

Totally agree... I think the real problem is that we often use AI to get things done faster without taking the time to understand what it actually generated. That understanding is probably the most valuable part of the learning process.

How do you think beginners can avoid falling into that habit ?