Start Coding

Topics

Swift Sendable Protocol

The Sendable protocol is a crucial feature in Swift's concurrency model. It helps ensure data safety when working with actors and concurrent tasks.

What is the Sendable Protocol?

Introduced in Swift 5.5, the Sendable protocol marks types that can be safely shared across concurrent contexts. It's a key component in preventing data races and maintaining thread safety.

Purpose and Benefits

  • Ensures data integrity in concurrent environments
  • Prevents accidental sharing of mutable state across actor boundaries
  • Helps catch potential concurrency issues at compile-time

Conforming to Sendable

Many Swift standard library types automatically conform to Sendable. For custom types, you can explicitly conform to the protocol:

struct MyStruct: Sendable {
    let immutableProperty: Int
    // All stored properties must be Sendable
}

class MyClass: Sendable {
    let immutableProperty: String
    // Classes must be marked as 'final' to conform to Sendable
}

Sendable Requirements

To conform to Sendable, types must meet certain criteria:

  • Value types (structs, enums) with all Sendable properties
  • Final classes with all immutable, Sendable properties
  • Actor types (automatically Sendable)
  • @Sendable closures

Using Sendable with Actors

Sendable is particularly important when working with Swift Actors. It ensures that data passed between actors is safe for concurrent access:

actor DataManager {
    func process(data: some Sendable) {
        // Safe to use 'data' here
    }
}

let manager = DataManager()
let safeData = SafeData() // Conforms to Sendable
await manager.process(data: safeData) // Compiles successfully

@Sendable Closures

Closures can be marked as @Sendable to indicate they're safe for concurrent execution:

let sendableClosure: @Sendable () -> Void = {
    print("This closure is safe for concurrent contexts")
}

Task {
    await someActor.performWork(sendableClosure)
}

Best Practices

  • Use Sendable for types shared across actor boundaries
  • Prefer immutable properties in Sendable types
  • Leverage compiler checks to catch Sendable violations early
  • Consider using property wrappers like @frozen for performance optimization

Conclusion

The Sendable protocol is a powerful tool in Swift's concurrency toolkit. By leveraging Sendable, developers can write safer, more robust concurrent code. It works hand-in-hand with other concurrency features like async/await and actors to create efficient, thread-safe applications.