ios - setPropertiesToFetch returns 'Invalid keypath expression' error -
i trying use group-by core data functionality in basic form:
- (nsfetchedresultscontroller *)fetchedresultscontroller { nsmanagedobjectcontext *managedobjectcontext = [self managedobjectcontext]; if (_fetchedresultscontroller != nil) { return _fetchedresultscontroller; } nsexpression *reviewnumkeyexpression = [nsexpression expressionforkeypath:@"review_num"]; nsexpression *reviewmaxexpression = [nsexpression expressionforfunction:@"max:" arguments:[nsarray arraywithobject:reviewnumkeyexpression]]; nsexpressiondescription *maxreviewed = [[nsexpressiondescription alloc] init]; [maxreviewed setname:@"maxreview"]; [maxreviewed setexpression:reviewmaxexpression]; [maxreviewed setexpressionresulttype:nsinteger32attributetype]; nsfetchrequest *fetchrequest = [[nsfetchrequest alloc] init]; nsentitydescription *entity = [nsentitydescription entityforname:@"group" inmanagedobjectcontext:managedobjectcontext]; [fetchrequest setresulttype:nsdictionaryresulttype]; [fetchrequest setreturnsdistinctresults:yes]; [fetchrequest setentity:entity]; nsattributedescription *groupid = [entity.propertiesbyname objectforkey:@"group_id"]; nsarray *propertiestofetch = [nsarray arraywithobjects:groupid, maxreviewed, nil]; [fetchrequest setpropertiestofetch:propertiestofetch]; [fetchrequest setpropertiestogroupby:propertiestofetch]; ...
when running code, following error:
terminating app due uncaught exception 'nsinvalidargumentexception', reason: 'invalid keypath expression ((), name maxreview, isoptional 1, istransient 0, entity (null), renamingidentifier maxdepartment, validation predicates ( ), warnings ( ), versionhashmodifier (null) userinfo { }) passed setpropertiestofetch
i guess missing api, appreciated!
i think error message wrong: problem in setpropertiestogroupby
, not setpropertiestofetch
. cannot group computed value (maxreview
), doubt want to: if want maximum value of review_num
each groupid
, need groupid
in propertiestogroupby
:
nsattributedescription *groupid = [entity.propertiesbyname objectforkey:@"group_id"]; nsarray *propertiestofetch = [nsarray arraywithobjects:groupid, maxreviewed, nil]; nsarray *propertiestogroupby = [nsarray arraywithobjects:groupid, nil]; [fetchrequest setpropertiestofetch:propertiestofetch]; [fetchrequest setpropertiestogroupby:propertiestogroupby];
Comments
Post a Comment