ios - How to write a Core Data predicate for objects which are related to an object in a to-many relationship -
entitya
has to-many relationship entityb
(and entityb
has to-one relationship entitya
). have array of entityb
s (or more accurately, have nsarray
contains instances of nsmanagedobject
represent entityb
). want create nsfetchrequest
fetch entitya
s have relationship @ least 1 of entityb
s in array. how write predicate fetch request?
the following works, think sub-optimal; it's hard grok , i'm sure there must better way of expressing this:
nsarray *entitybs = ...; nsmutablearray *containsentitybsubpredicates = [nsmutablearray new]; (nsmanagedobject *entityb in entitybs) { [containsentitybsubpredicates addobject:[nspredicate predicatewithformat:@"%@ in entitybs", entityb]]; } nspredicate *containsentitybspredicate = [nscompoundpredicate orpredicatewithsubpredicates:containsentitybsubpredicates];
i've tried this, doesn't work:
nsarray *entitybs = ...; nspredicate *containsentitybspredicate = [nspredicate predicatewithformat:@"any %@ in entitybs", entitybs];
am missing simpler solution?
you there predicate, switch parameters:
[nspredicate predicatewithformat:@"any entitybs in %@", entitybarray];
look @ apple's example code in
here further explanation.
Comments
Post a Comment