how do i use @mainactor properly
Asked on 2024-07-30
1 search
To use @MainActor properly in Swift, you can follow these guidelines based on the content from the WWDC session "Migrate your app to Swift 6":
-
Annotate Methods and Properties: You can annotate methods and properties with
@MainActorto ensure they are executed on the main actor. This is particularly useful for UI-related code that must run on the main thread.@MainActor func updateUI() { // UI update code } -
Assume Isolated: If you know a function is always called on the main actor, you can use
MainActor.assumeIsolatedto inform the compiler without starting a new task. This helps avoid unnecessary context switching and ensures the code runs on the main actor.func someFunction() { MainActor.assumeIsolated { // Code that runs on the main actor } }This approach is useful when you are certain about the execution context, such as when dealing with delegate callbacks that are guaranteed to be on the main actor.
-
Non-Isolated Methods: For methods that do not need to be isolated to the main actor, you can declare them as non-isolated. This is useful for callbacks that do not guarantee the execution context.
nonisolated func someCallback() { // Code that does not need to run on the main actor } -
Compiler Fixes: The Swift compiler can help identify where
@MainActorannotations are needed. For example, if you get a compiler error indicating that a method should be on the main actor, you can apply the suggested fix to annotate the method.@MainActor func someMainActorMethod() { // Code that must run on the main actor } -
Protocols and Delegates: Many protocols and delegates, especially those related to UI frameworks like SwiftUI, are annotated with
@MainActor. This ensures that their methods are called on the main actor.protocol SomeDelegate: @MainActor { func didUpdateData() }
For more detailed information, you can refer to the session Migrate your app to Swift 6 at the timestamp 24:15.

A Swift Tour: Explore Swift’s features and design
Learn the essential features and design philosophy of the Swift programming language. We’ll explore how to model data, handle errors, use protocols, write concurrent code, and more while building up a Swift package that has a library, an HTTP server, and a command line client. Whether you’re just beginning your Swift journey or have been with us from the start, this talk will help you get the most out of the language.

What’s new in SwiftUI
Learn how you can use SwiftUI to build great apps for any Apple platform. Explore a fresh new look and feel for tabs and documents on iPadOS. Improve your window management with new windowing APIs, and gain more control over immersive spaces and volumes in your visionOS apps. We’ll also take you through other exciting refinements that help you make expressive charts, customize and layout text, and so much more.

Migrate your app to Swift 6
Experience Swift 6 migration in action as we update an existing sample app. Learn how to migrate incrementally, module by module, and how the compiler helps you identify code that’s at risk of data races. Discover different techniques for ensuring clear isolation boundaries and eliminating concurrent access to shared mutable state.
