Fails with "to-many key not allowed here"
// parent.children?.contains(where: {
// $0.name == "Abbiejean"
// }) != nil
parent.children.flatMap { children in
children.contains(where: { $0.name == "Abbijean" })
} == true
How are we supposed to query on relationships? This is a huge problem.
This is a major limitation blocking migration of CoreData to SwiftData.
We can do this with NSPredicate
:
let moodAnalysis = NSPredicate(format: "ANY moodAnalysis.labels.title == %@", label.description)
let stateOfMinds = NSPredicate(format: "SUBQUERY(stateOfMinds, $x, SUBQUERY($x.labels, $y, $y.title == %@).@count > 0).@count > 0", label.description)
The accepted answer on stack overflow is: you can't
Document says that optionals are allowed in predicates The SwiftData team has made a big show of saying that we can use idiomatic swift for our predicates. But we cannot even filter on relationships when the container is backed by CloudKit...
That should be a HUGE warning in the documentation. "For those of you who are considering a costly refactor from CoreData to SwiftData, and are currently using CloudKit, all relationships are mandatory optional arrays, and you can't write predicates on them"