Hello, I have a Task model in my application which has an optional many to many relationship to a User model.
task.assignedUsers
user.tasks
I am looking to construct a SwiftData predicate to fetch tasks which either have no assigned users or assigned users does not contain specific user.
Here is a partially working predicate I have now:
static func assignedToOthersPredicate() -> Predicate<Task> {
let currentUserGUID = User.currentUserGUID
return #Predicate<Task> { task in
task.assignedUsers.flatMap { users in
users.contains(where: { $0.guid != currentUserGUID })
} == true
}
}
This only returns tasks assigned to others, but not those which have no assigned users.
If combine it with this:
static func notAssignedPredicate() -> Predicate<Task> {
return #Predicate<Task> { task in
task.assignedUsers == nil
}
}
Then I get a run time crash: "to-many key not allowed here"
What is the proper way to do this?
Thanks.