Sendable protocol real world example

Asked on 08/04/2024

1 search

A real-world example of the Sendable protocol can be found in the session Migrate your app to Swift 6. In this session, the presenter discusses a scenario involving a public type called Drink in a coffee tracking app.

Here's a summary of the example:

  1. Context: The Drink type is shared between the main app and a coffee tracker extension. Since it is a public type, Swift does not infer its sendability automatically.
  2. Problem: Without marking Drink as Sendable, there are multiple warnings because the type is used across different concurrency domains (e.g., between actors).
  3. Solution: By explicitly marking the Drink type as Sendable, the warnings are resolved, ensuring that the type can be safely shared between different actors without risking data races.

This example illustrates how marking a type as Sendable can help manage concurrency and ensure data-race safety in a Swift application. For more details, you can watch the session Migrate your app to Swift 6.