views:

283

answers:

1

Hi all,

I have a data model with a many-to-many relationship like EntityA <-->> EntityB <<--> EntityC. I used to query EntityA with different search criteria and I use NSCompoundPredicate with an array of NSPredicates. On one of the predicate I wanted to query EntityA using EntityC. I tried to use the following SUBQUERY but it 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 anything I'm missing. I would appreciate any help.

Sarah

A: 

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

I think your making this overly complicated. You should simplify with keypaths. Each EntityB has a to-one relationship to a EntityC so to search on EntityB objects all you need to do is check EntityB.entityCRelationshipName.entityCAttribute. So something like:

ALL B.EntityC.name like %@

In any case, (if I'm reading the predicate correctly) this:

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

can only return a single object instead of a collection. Therefore, the predicate will not be able to perform a count on it. That is what your error message means.

I suggest using the predicate editor in the Data Model editor to hash out the predicates before you store them.

TechZen
Thanks for you reply, I have used you approach before but got the following exception *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unsupported predicate ALL B.EntityC.name LIKE "name"' and I used the predicate editor for constructing it.
Sarah
Try `ANY entityB.entityC.name LIKE[c] $aName` and provide the "aName" variable. Make sure that your relationship names aren't literally the same as the entity names.
TechZen
ANY entityB.entityC.name LIKE[c] $aName worked very good, thanks
Sarah