protect mutable state

Asked on 2026-04-24

1 search

To protect mutable state in Swift, especially in the context of concurrency, several strategies are discussed in the WWDC sessions:

  1. Main Actor: Annotating an entire class with the main actor can protect all of its mutable state. This is particularly useful in projects that don't have a lot of concurrent tasks. The main actor ensures that global and static variables are safe from data races by default. This approach is recommended for apps, scripts, and other executable targets where most work is done on the main actor, and concurrent code is encapsulated within specific types or files. This feature is opt-in and helps reduce concurrency annotations in mostly single-threaded code. (What’s new in Swift)

  2. Sendable Value Types: When sharing mutable state between concurrent code, using sendable value types can prevent data races. Sendable types ensure that the state is protected from concurrent accesses. If a type is not sendable, you might need to manually add synchronization or use actors to encapsulate shared mutable state. (A Swift Tour: Explore Swift’s features and design)

  3. Swift 6 Language Mode: This mode introduces full enforcement of data isolation, preventing accidental sharing of state between tasks and actors. It helps catch concurrency bugs at compile time, improving the quality of your app. (Migrate your app to Swift 6)

  4. Isolated Conformances: These are used when a conformance needs main actor state, ensuring that it is only used on the main actor. This helps solve data race safety issues when the code indicates concurrent use. (What’s new in Swift)

  5. Concurrency Features in Swift 6: Swift 6 provides tools to prevent data races, such as isolating state to actors and using structured concurrency to control asynchronous code. (Code-along: Elevate an app with Swift concurrency)

These strategies collectively help in managing and protecting mutable state in Swift applications, especially when dealing with concurrency.

What’s new in Swift

What’s new in Swift

Join us for an update on Swift. We’ll talk about workflow improvements that make you more productive, and new and modernized library APIs for fundamental programming tasks. We’ll show examples of Swift adoption throughout more layers of the software stack. Finally, we’ll explore new language features for both improving approachability of concurrency, and achieving peak performance when you need it.

Code-along: Elevate an app with Swift concurrency

Code-along: Elevate an app with Swift concurrency

Learn how to optimize your app’s user experience with Swift concurrency as we update an existing sample app. We’ll start with a main-actor app, then gradually introduce asynchronous code as we need to. We’ll use tasks to optimize code running on the main actor, and discover how to parallelize code by offloading work to the background. We’ll explore what data-race safety provides, and work through interpreting and fixing data-race safety errors. Finally, we’ll show how you can make the most out of structured concurrency in the context of an app.

A Swift Tour: Explore Swift’s features and design

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.