Wednesday, 15 August 2012

ios - CoreData NSPredicate with two one-to-many relations -



ios - CoreData NSPredicate with two one-to-many relations -

i have coredata model info construction restricting info based on grouping user belongs to:

category <->> info <->> groups.

i have nsset of usergroups objects. want able filter categories based on nsset of grouping objects, such if category not contain pieces of info have groups within nsset, not returned predicate.

for info can

[nspredicate predicatewithformat:@"any groups in (%@)",groups];

for categories i've tried next crash:

[nspredicate predicatewithformat:@"any information.groups in (%@)",groups];

but need write predicate @ category level. programming under assumption info in info set sufficiently big cannot pull them of them out , process them find categories. want create predicate fetch categories relevant user based on his/her groups.

thanks help!

the next predicate on category should work (assuming information to-many relationship category information):

[nspredicate predicatewithformat:@"subquery(information, $i, $i.groups in %@).@count > 0",groups];

alternatively, utilize inverse relationships:

// set of grouping objects: nsset *groups = ...; // info objects related grouping object: nsset *information = [groups valueforkey:@"information"] ; // category objects related info object: nsset *categories = [information valueforkey:@"category"];

which can combined to

nsset *categories = [groups valueforkeypath:@"information.category"];

a disadvantage of alternative solution might creates intermediate set of groups in memory.

ios core-data many-to-many nspredicate any

No comments:

Post a Comment