swiftswiftdata

SwiftData #Predicate in Swift 6 language mode


I'm trying to migrate over to the Swift 6 language mode, but the biggest issue I'm facing is that any use of SwiftData #Predicate or SortDescriptor results in this warning from the compiler:

Type 'ReferenceWritableKeyPath<GuruSchemaV2.Rubric, Bool>' does not conform to the 'Sendable' protocol; this is an error in the Swift 6 language mode

Here is an example predicate, from a static method on the Rubric type:

static func notArchived() -> Predicate<Rubric> {
    return #Predicate<Rubric> { rubric in
        !rubric.archived
    }
 }

And the error highlights line 5 of the expanded macro:

Foundation.Predicate<Rubric>({ rubric in
    PredicateExpressions.build_Negation(
        PredicateExpressions.build_KeyPath(
            root: PredicateExpressions.build_Arg(rubric),
            keyPath: \.archived     // <- Warning here
        )
    )
})

What is the correct way to reference properties of a model type using #Predicate?


Edit: Rubric is a large class but here is the declaration and relevant properties:

@Model
class Rubric: Identifiable, Comparable {
    init() { ... }
    
    var subject: String = ""
    var unit: String = ""
    var archived: Bool = false

    // ...
}

Solution

  • This is likely because the "default actor isolation" project setting is set to the main actor in your project settings:

    enter image description here

    This is the default when you create a new project using Xcode 26 beta 1.

    This means that Rubric is implicitly isolated to the main actor. This makes its key paths not sendable, because the code must be running on the main actor to access those key paths.

    You can either change that setting to "nonisolated", or make your @Model nonisolated.

    @Model
    nonisolated class Rubric { ... }