3

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

    // ...
}
4
  • 1
    I still cannot reproduce. It is your responsibility to narrow down the problem to a minimal reproducible example. In this particular case, you should be able to create some code that can be directly copy-pasted into a new Xcode project and have the problem be reproduced. Commented Jun 15 at 19:51
  • I can't reproduce this either, maybe it's some setting in your project/target. What if you create a new SwiftData project for Swift 6 and copy the Rubric model to it, will it build without warnings? Commented Jun 15 at 20:45
  • I have this problem too, with Xcode 26 beta 1. Someone seems to have it solved here but I still get the error. It says it’s because ReferenceWritableKeyPath does not conform to Sendable. @Sweeper you can copy & paste the code into a new project and correct as needed (remove Identifiable, Comparable, and the filler ellipses). Commented Jun 16 at 13:20
  • @sven7 I can finally reproduce this by creating a new project in Xcode 26, instead of doing this in one of my old projects. This made me suspect that this is a project setting issue, and Xcode 26 apparently sets the default actor isolation to the main actor. Commented Jun 16 at 13:32

1 Answer 1

5

Update: In the official release of Xcode 26, this is no longer a problem. Now, the main actor isolation inference will ignore @Model classes, as their "primary definition directly conforms to a protocol that inherits SendableMetatype", as per the SE proposal. The rest of the answer still applies in general.


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 { ... }
Sign up to request clarification or add additional context in comments.

3 Comments

@JoakimDanielson I can reproduce on both iOS and macOS. Though, interestingly I can only reproduce this in Swift 6 mode.
Thanks for the explanation. Leaving the Default Actor Isolation setting at MainActor seems reasonable overall as it supports safety. I also don’t want to add nonisolated to all my model classes and enums for the same reason. But then I wonder -- is there a way to make the predicate access isolated properties? I feel like my hand is forced to give up on isolation just to make predicates work.
@sven7 SwiftData models should absolutely be nonisolated. They can be fetched from any model context in any isolation. Predicate itself is Sendable, so no, it cannot be constructed using non-sendable things. If it contains those non-sendable things, then it will no longer be sendable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.