what can be marked as @MainActor?
Generated on 8/6/2024
1 search
In Swift, you can mark various elements with the @MainActor
attribute to ensure they are executed on the main actor, which is typically associated with the main thread. Here are some examples of what can be marked as @MainActor
:
-
Methods: You can mark methods to ensure they run on the main actor. This is useful for UI updates or other tasks that must be performed on the main thread.
- Example:
@MainActor func updateUI() { ... }
- Example:
-
Protocols: Protocols can be annotated with
@MainActor
to ensure that all conforming types and their methods are executed on the main actor.- Example:
@MainActor protocol MyProtocol { func doSomething() }
- Example:
-
Classes and Structs: Entire classes or structs can be marked with
@MainActor
to ensure all their methods and properties are accessed on the main actor.- Example:
@MainActor class MyViewController: UIViewController { ... }
- Example:
-
Global Instances and Functions: You can also mark global instances and functions to ensure they are accessed on the main actor.
- Example:
@MainActor let sharedInstance = MySingleton()
- Example:
In the context of the WWDC sessions, the @MainActor
attribute is particularly relevant for SwiftUI views and delegate protocols. For instance, the SwiftUI.View
protocol is implicitly isolated to the main actor by default in Swift 6, so you no longer need to explicitly mark your views with @MainActor
(What’s new in SwiftUI).
Additionally, when dealing with delegate callbacks, it's important to understand the concurrency guarantees. Many UI frameworks guarantee that callbacks will be on the main thread, and you can use @MainActor
to enforce this (Migrate your app to Swift 6).
For more detailed information, you can refer to the session Migrate your app to Swift 6.
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.