when to use sendable

Generated on 8/9/2024

1 search

In Swift, the Sendable protocol is used to ensure that a type can be safely transferred between different concurrency domains, such as actors or threads. Here are some key points on when to use Sendable:

  1. Public Types: If you have a public type that you share outside of your module, you need to explicitly mark it as Sendable. Swift does not infer sendability for public types automatically because marking a type as Sendable is a guarantee to your clients that the type contains no mutable state. This is to avoid locking in sendability prematurely (Migrate your app to Swift 6).

  2. Concurrency Safety: When you need to ensure data-race safety in your program, especially when values are shared between concurrency domains. A Sendable value protects its state from concurrent accesses. For example, a type might qualify as Sendable if it acquires a lock while reading and writing mutable state (A Swift Tour: Explore Swift’s features and design).

  3. Actors: When using actors, which are reference types that can encapsulate shared mutable state, you might need to make certain types Sendable to ensure safe interaction between different actors (Migrate your app to Swift 6).

  4. Internal Types: For internal types, Swift can automatically consider them Sendable if they meet the criteria. However, for public types, you need to explicitly add Sendable conformance (Migrate your app to Swift 6).

Relevant Sessions

These sessions provide detailed explanations and examples of when and how to use Sendable in your Swift applications.

Consume noncopyable types in Swift

Consume noncopyable types in Swift

Get started with noncopyable types in Swift. Discover what copying means in Swift, when you might want to use a noncopyable type, and how value ownership lets you state your intentions clearly.

What’s new in App Intents

What’s new in App Intents

Learn about improvements and all-new features with App Intents, and discover how this framework can help you expose your app’s functionality to Siri, Spotlight, Shortcuts, and more. We’ll show you how to make your entities more meaningful to the platform with the Transferable API, File Representations, new IntentFile APIs, and Spotlight Indexing, opening up powerful functionality in Siri and the Shortcuts app. Empower your intents to take people deep into your app with URL Representable Entities. Explore new techniques to model your entities and intents with new APIs for error handling and union values.

Migrate your app to Swift 6

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

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.