0
votes

I have an Entity called Group. Which has a to-many relationship to Group called parentGroup.

I want to query CD for all top level groups. (Ones that don't have a parent group).

I can't figure out what the predicate should be. I have tried:

fetch.predicate = NSPredicate(format: "parentGroup" == nil)

NSPredicate(format: "%K == nil", #keyPath(Group.parentGroup))

NSPredicate(format: "parentGroup" == %@, nil)

An old Obj-C post on a similar topic that didn't work: iPhone SDK Core Data: Fetch all entities with a nil relationship?

Thanks

1
Have you implemented an inverse for the relationship? I just wonder whether this is a side effect of having no inverse. - pbasdf
What results do you get with these predicates? In what way are they failing? - Tom Harrington
Yeah I have an inverse. @TomHarrington the predicates aren't valid code or return all items even if they have a parent group. (Yes I'm adding the predicate to the fetch request.) - aestusLabs

1 Answers

0
votes

Here is the solution. The other predicates were failing because it was a to-many relationship.

fetch.predicate = NSPredicate(format: "parents.@count == 0")