Is it safe to mark SwiftData (@Model) classes as Sendable?

The Sendable documentation says we can mark reference types as Sendable if they "internally manage access to their state."

Adding Sendable conformance to my SwiftData classes silences warnings such as the following: "Non-sendable type '[Item]' returned by implicitly asynchronous call to nonisolated function cannot cross actor boundary"

@Model final class Item: Sendable {
  var sampleProperty = ""
}

My understanding is that the compiler would complain if adding explicit Sendable conformance to a swift data model was breaking concurrency rules, but I wanted to check with the community to see what everyone thinks.

Best, Taylor

Post not yet marked as solved Up vote post of taychap Down vote post of taychap
867 views
Add a Comment

Replies

model classes are not internally thread safe, so you should not do this. You can pass the persistentModelID between actors. PersistentIdentifier is sendable.