Objective-c. Fetching distinct values of attribute from core data -
i have run problem can not solve. have "database" - read core data - have attribute holds value , level.
that
value -------- level
55 -------------4
33 -------------4
50 -------------5
70 -------------6
44 -------------5
want extract values level 5 , add them together. how can achieve ?i did found "fetch distinct values" on apple dev site, apply extracting values 1 attribute.
help appreciated, thank you. if have missed similar topic please provide me link.
you can use following fetch request:
// fetch request entity: nsfetchrequest *request = [nsfetchrequest fetchrequestwithentityname:@"entity"]; [request setresulttype:nsdictionaryresulttype]; // restrict result "level == 5": nspredicate *predicate = [nspredicate predicatewithformat:@"level == %d", 5]; [request setpredicate:predicate]; // expression description "@sum.value": nsexpression *sumexpression = [nsexpression expressionforkeypath:@"@sum.value"]; nsexpressiondescription *expressiondescription = [[nsexpressiondescription alloc] init]; [expressiondescription setname:@"sumvalue"]; [expressiondescription setexpression:sumexpression]; [expressiondescription setexpressionresulttype:nsinteger32attributetype]; [request setpropertiestofetch:@[expressiondescription]]; nsarray *result = [context executefetchrequest:request error:&error];
the result data is
( { sumvalue = 94; } )
i.e. array containing 1 dictionary sum of values level=5.
Comments
Post a Comment