Multiple Relationship Master Data NSPredicate

I have a many-to-many data model, for example EntityA <-->> EntityB <<--> EntityC. I used a query EntityAwith different search criteria and I use NSCompoundPredicatewith an NSPredicates array . In one of the predicates I wanted to query EntityAwith EntityC. I tried using the following SUBQUERY, but that did not work.

searchPredicate=[NSPredicate predicateWithFormat:@"(0 != SUBQUERY(EntityB, $B, (0 != SUBQUERY($B.EntityC, $EntityC, $EntityC.name like %@).@count)).@count)", name]

And I got the following exception:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason:
'Can't perform collection evaluate with non-collection object.'

Is there something that I am missing. I would appreciate any help.

Sarah

+1
source share
1 answer

(I had a lot of trouble puzzling your predicate, so take this with salt.)

I think you make it too complicated. You have to simplify with keys. Each EntityB has one relationship to EntityC, so to find EntityB objects all you have to do is check EntityB.entityCRelationshipName.entityCAttribute. So something like:

ALL B.EntityC.name like %@

In any case (if I read the predicate correctly):

SUBQUERY($B.EntityC, $EntityC, $EntityC.name like %@)

can return only one object instead of a collection. Therefore, the predicate cannot perform the calculation. This means your error message.

I suggest using the predicate editor in the data model editor to hash the predicates before saving them.

+3
source

All Articles