TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
@MainActor attribute is a global actor annotation in Swift’s structured concurrency model that statically guarantees a declaration executes exclusively on the main thread. By applying this attribute, the Swift compiler enforces thread-safety at compile time, ensuring that state mutations and function invocations associated with the annotated entity are serialized through the main dispatch queue.
Under the hood, @MainActor is a singleton actor conforming to the GlobalActor protocol. It utilizes a custom executor that routes all of its isolated tasks to DispatchQueue.main.
Syntax and Application
The attribute can be applied to various declarations, including classes, structs, protocols, functions, properties, and closures. When applied to a type, all of its properties and methods implicitly inherit the main actor isolation.Isolation Domains and Cross-Actor Access
When a declaration is annotated with@MainActor, it enters the main actor’s isolation domain. The compiler enforces strict rules regarding how this domain is accessed:
- Synchronous Access: Code already executing within the
@MainActorisolation domain can access other@MainActordeclarations synchronously. - Asynchronous Access: Code executing outside the
@MainActorisolation domain (e.g., from a background task or a different actor) must cross the isolation boundary asynchronously using theawaitkeyword.
Opting Out with nonisolated
When an entire type is annotated with @MainActor, you can selectively exclude specific members from the isolation domain using the nonisolated keyword. This allows those specific members to be accessed synchronously from any thread.
The nonisolated keyword cannot be applied to any stored properties (var or let). It is restricted to methods and computed properties. While immutable stored properties (let) that are Sendable and initialized inline are implicitly accessible from outside the isolation domain, explicitly annotating them with nonisolated is invalid Swift syntax and will produce a compiler error.
Closure Isolation
Closures can be explicitly bound to the main actor. This is frequently used when spawning unstructured tasks that require main thread execution.Protocol Conformance and Inheritance
If a protocol is annotated with@MainActor, a type that conforms to that protocol infers @MainActor isolation only if the conformance is declared on the primary type declaration. If the conformance is added via an extension, only the members declared within that specific extension infer the isolation; the rest of the type remains unisolated.
@MainActor, all of its subclasses inherit the attribute. Subclasses cannot remove the @MainActor isolation inherited from a superclass.
Explicit Execution and Assertions
TheMainActor type provides specific APIs for executing code and asserting isolation dynamically when the compiler cannot statically verify the context.
MainActor.run
This method executes a block of code on the main actor from an async context without spawning a new unstructured task. It is used to transition execution to the main thread and group multiple isolated operations.
MainActor.assumeIsolated
This method synchronously asserts that the current execution context is the main actor. It is used when bridging with older callback-based APIs or delegates where the compiler cannot statically prove the execution context, but the runtime environment guarantees it. The closure receives an isolated reference to the MainActor as its argument, which must be explicitly handled or ignored (e.g., using _ in). If the code is executed off the main actor, this method triggers a fatal runtime crash.
Master Swift with Deep Grasping Methodology!Learn More





