I’m working on a SwiftUI app using SwiftData and recently enabled Swift 6 in my project. After doing so, I started getting the following error when using a SortDescriptor with a key path:
Type 'ReferenceWritableKeyPath<ServerDomain, Int>' does not conform to the 'Sendable' protocol
This happens in a SwiftData @Query like this:
@Query(sort: [SortDescriptor(\ServerDomain.sortOrder)])
private var servers: [ServerDomain]
Here’s a simplified version of my model:
@Model
class ServerDomain {
var id: UUID
var name: String
var sortOrder: Int
init(id: UUID = .init(), name: String, sortOrder: Int) {
self.id = id
self.name = name
self.sortOrder = sortOrder
}
}
It seems that since ReferenceWritableKeyPath is not Sendable by default, Swift 6’s stricter concurrency model now flags this as an error. I understand I can suppress the error using @unchecked Sendable, but I’d like to follow concurrency safety best practices if possible.
My question is:
How can I safely apply SortDescriptor with SwiftData models and Swift Concurrency in Swift 6, without violating Sendable constraints?
Is there a recommended approach for using key paths like this inside Swift concurrency-compatible contexts?
I tried marking my model as @unchecked Sendable, like this:
@Model
final class ServerDomain: @unchecked Sendable {
var id: UUID
var name: String
var sortOrder: Int
init(id: UUID = .init(), name: String, sortOrder: Int) {
self.id = id
self.name = name
self.sortOrder = sortOrder
}
}
However, this doesn’t resolve the issue — I’m still seeing the same compiler error:
Type 'ReferenceWritableKeyPath<ServerDomain, Int>' does not conform to the 'Sendable' protocol
It looks like marking the model class as @unchecked Sendable isn’t enough, since the error seems to be related to the key path itself rather than the model type directly.
If you have an example of how this should be done correctly in a Swift 6 + SwiftData setup — ideally without suppressing the compiler’s Sendable checks I’d really appreciate it.
@Query(sort: [SortDescriptor(\Item.timestamp)]) privatevar items: [Item]
Type 'ReferenceWritableKeyPath<Item, Date>' does not conform to the 'Sendable' protocol
Ah, there it is. Currently, MainActor-default-isolation does not work well with certain frameworks, these frameworks require some custom types to be nonisolated. You can check the related discussions here and here.
Thanks a lot, that really helped clarify things!
For now, I’ll go with the nonisolated workaround as it solves the issue cleanly in my case. Appreciate your help!