iphone - Core Data, filtering out child entities in result set -


so have typical 1:m relationship:

car can have many models 

i want car objects , model names begin 'a'.

i tried subquery:

nsfetchrequest *fetchrequest = [[nsfetchrequest alloc] init]; nsentitydescription *entity = [nsentitydescription entityforname:@"car" inmanagedobjectcontext:_context]; [fetchrequest setentity:entity];  nspredicate *predicate = [nspredicate predicatewithformat:@"subquery(models, $model, $model.name beginswith 'a').@count > 0"];     [fetchrequest setpredicate:predicate]; 

this return car long has model begins 'a'. that's fine, cars returned, returns models, , want ones start 'a'

but seems long operate on higher level entity (car), subquery filters cars , doesn't filter models @ all.

what i'm doing filtering models in inner loop (using nspredicate), i'd rather filtering on sql side.

ideas?

run fetch against models entity predicate:

[nspredicate predicatewithformat:@"name beginswith 'a'"]; 

from result array, nsarray *result, can of cars kvc:

[result valueforkey:@"car"]; 

assuming have 1:1 inverse relationships 1:m cars models relationship (you should, way; core data uses inverse relationships maintain consistency of object graph).

if want set of unique cars:

[result valueforkeypath:@"@distinctunionofobjects.car"]; 

or

nsset *cars = [nsset setwitharray:[result valueforkey:@"car"]]; 

Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -