Swift Data + Swift 6 Sendable-ity paradox

Xcode 16 beta 3

Assume a SwiftData model starts like this and has a few more properties like a name and creation date (these are immaterial to my main question.

@Model final class Batch: Identifiable, Sendable { @Attribute(.unique) var id: UUID //... more stuff

The combination of Swift 6 (or Swift 5 with warnings enabled) and SwiftData seem to provide a paradox:

Swift 6 complains when the id is a let:

Cannot expand accessors on variable declared with 'let'; this is an error in the Swift 6 language mode

Swift 6 complains when the id is a var:

Stored property '_id' of 'Sendable'-conforming class 'Batch' is mutable; this is an error in the Swift 6 language mode

Removing "Sendable" may be one solution but defeats the purpose and causes warnings elsewhere in the app about the model not being Sendable.

Is there an obvious fix?

Am I as a newbie (to the combination of Swift 6 and SwiftData) missing an entire architectural step of using ModelActor somewhere?

Answered by FPST in 800431022

You shouldn't mark your SwiftData model Sendable because it isn't. I suggest you use @MainActor. Instead of passing around your @Model you should only pass around the persistentModelID. If it works, you could also mark all of your views with SwiftData @MainActor. That might work for your project. Otherwise go with @ModelActor.

Accepted Answer

You shouldn't mark your SwiftData model Sendable because it isn't. I suggest you use @MainActor. Instead of passing around your @Model you should only pass around the persistentModelID. If it works, you could also mark all of your views with SwiftData @MainActor. That might work for your project. Otherwise go with @ModelActor.

Swift Data + Swift 6 Sendable-ity paradox
 
 
Q