ios - NSSortDescriptor - Sort descriptor based on another array -
i hava core data application. , want fetch 1 kind of object, user. user have property userid.
i have array userids, [1, 4, 3, 5]. , create nssortdescriptor sorts userobjects based on order of userids in array.
is possible, , how should that?
update
i have tried following.
i added transformable property user object, store user ids array.
i have tried following sort descriptor:
sortdescriptor = [[nssortdescriptor alloc] initwithkey:@"userid" ascending:yes comparator:^nscomparisonresult(id obj1, id obj2) { nsuinteger idx1 = [self.user.followingids indexofobject:[obj1 valueforkey:@"userid"]]; nsuinteger idx2 = [self.user.followingids indexofobject:[obj2 valueforkey:@"userid"]]; return idx1 - idx2; }];
and i'm getting following error:
serious application error. exception caught during core data change processing. bug within observer of nsmanagedobjectcontextobjectsdidchangenotification. [<__nscfnumber 0xa337400> valueforundefinedkey:]: class not key value coding-compliant key userid. userinfo { nstargetobjectuserinfokey = 2502; nsunknownuserinfokey = userid; } *** terminating app due uncaught exception 'nsunknownkeyexception', reason: '[<__nscfnumber 0xa337400> valueforundefinedkey:]: class not key value coding- compliant key userid.' update 2
would add relationship followers between user objects. ideas how relationship should look? see attached image. correct?

this can not done sort descriptor, have apply custom comparator function after fetching results:
nsarray *userids = ...; // e.g. @[@1, @4, @3, @5] nsarray *results = ...; // result of fetch request nsarray *sorted = [results sortedarrayusingcomparator:^nscomparisonresult(id obj1, id obj2) { nsuinteger idx1 = [userids indexofobject:[obj1 valueforkey:@"userid"]]; nsuinteger idx2 = [userids indexofobject:[obj2 valueforkey:@"userid"]]; return idx1 - idx2; }];
Comments
Post a Comment