What’s the proper way to use actors and @Observable classes when building services and view models?
Generated on 7/30/2024
1 search
To properly use actors and @Observable
classes when building services and view models, you should follow these guidelines:
-
Use Actors for Concurrency Safety: Actors in Swift are reference types that automatically protect their state by serializing accesses. This ensures that only a single task can execute at a time on an actor, which helps prevent data races. For example, if you have a
UserStore
that needs to be accessed from different concurrency domains, making it an actor will synchronize accesses and eliminate concurrency issues (A Swift Tour: Explore Swift’s features and design). -
Main Actor for UI-Related Code: When dealing with UI-related code, such as views and models, you should run them on the main actor. This ensures that all UI updates are performed on the main thread, which is crucial for maintaining a responsive user interface. For instance, in the Coffee Tracker app, views and models were set to run on the main actor, while background operations were performed on dedicated actors (Migrate your app to Swift 6).
-
Isolate Methods Appropriately: If you have methods that need to run on a specific actor, you can declare them as non-isolated or use the
assume isolated
method to inform the compiler about the concurrency context. This is useful for callbacks that need to run on the main actor but are called from different concurrency contexts (Migrate your app to Swift 6). -
Use Value Types for Communication: When passing data between actors, use value types to avoid shared mutable state. This helps prevent data races. For example, in the Coffee Tracker app, the
Drink
type was a struct with immutable properties, making it safe to pass between actors (Migrate your app to Swift 6). -
Annotations for Main Actor Isolation: Many delegates and protocols, especially those related to SwiftUI, are annotated to operate only on the main actor. This reduces the need for explicit main actor annotations in your code. For instance, the SwiftUI view protocol is implicitly isolated to the main actor by default (What’s new in SwiftUI).
By following these guidelines, you can effectively manage concurrency in your Swift applications, ensuring that your services and view models are both safe and performant. For more detailed information, you can refer to the sessions "Migrate your app to Swift 6" and "A Swift Tour: Explore Swift’s features and design" from WWDC 2024.

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.

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.