what's sendable and give an example of it and compare it with a usecase where we are not using sendable

Generated on 12/10/2024

1 search

In Swift, "Sendable" is a protocol that ensures a type can be safely transferred across concurrency domains without causing data races. A type is considered sendable if it protects its state from concurrent accesses, which is crucial for maintaining data integrity in concurrent programming.

Example of Sendable

Consider a scenario where you have a Drink type that you want to send between different actors in a Swift application. If Drink is a struct with immutable properties, it can be marked as sendable, allowing it to be safely shared between actors without risking data races. Here's a simplified example:

struct Drink: Sendable {
    let name: String
    let size: Int
}

In this case, marking Drink as Sendable ensures that it can be safely passed between actors, as it contains only immutable state.

Use Case Without Sendable

If you have a type that contains mutable state, such as a reference type with properties that can change, it might not be safe to mark it as sendable. For instance, an Objective-C type that stores mutable state in a reference type might not be sendable. In such cases, you would need to ensure thread safety manually, perhaps by using locks or other synchronization mechanisms, or by redesigning the type to avoid shared mutable state.

Comparison

  • With Sendable: Types are explicitly marked as safe for concurrent use, reducing the risk of data races and making the code easier to reason about.
  • Without Sendable: You must manually ensure thread safety, which can be error-prone and complex, especially in large projects.

For more details on how Swift handles sendability and data race safety, you can refer to the session Migrate your app to Swift 6.

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.

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.

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.