What is Sendable protocol and how it relates to Actors in swift?

Asked on 08/23/2024

1 search

The Sendable protocol in Swift is a key concept related to concurrency, particularly when working with actors. It ensures that types can be safely transferred across different concurrency domains without causing data races. In Swift, a type is considered sendable if it can protect its state from concurrent accesses, which is crucial for maintaining data integrity in concurrent programming.

Actors in Swift are reference types that encapsulate shared mutable state and automatically protect their state by serializing accesses. This means that only one task can execute at a time on an actor, preventing data races. The Sendable protocol complements actors by ensuring that any data shared between actors is safe from concurrent modifications.

In Swift 6, the language enforces data isolation more strictly, requiring explicit conformance to Sendable for public types. This is because marking a type as sendable is a guarantee to clients that the type contains no mutable state that could lead to data races. For internal types, Swift can infer sendability automatically, but for public types, developers must explicitly declare them as sendable to avoid potential concurrency issues.

For more details, you can refer to the session Migrate your app to Swift 6 (32:27) where this topic is discussed.