ios - Can't set relationship object to another object in CoreData -
i have 2 entities: goal , category. added relationship "category" goal entity. want user can choose category list , category added relation goal object. getting error: -[__nssingleobjectseti managedobjectcontext]: unrecognized selector sent instance 0x138a22fa0
my code:
- (bool)save { // category object nsmanagedobject *objcategory = [self getcategoryobjectwithid:@"1"]; if (!objcategory) { nslog(@"error objcategory fetching!"); return; } nsentitydescription *entity = [nsentitydescription entityforname:@"goal" inmanagedobjectcontext:[coredatawrapper mymanagedcontext]]; nsmanagedobject *goal = [[nsmanagedobject alloc] initwithentity:entity insertintomanagedobjectcontext:[coredatawrapper mymanagedcontext]]; [goal setvalue:strid forkey:@"id"]; [goal setvalue:[nsset setwithobject:objcategory] forkey:@"category"]; // here error return [coredatawrapper savemycontext]; } - (nsmanagedobject *)getcategoryobjectwithid:(nsstring *)catid { // fetching nsfetchrequest *fetchrequest = [[nsfetchrequest alloc] initwithentityname:@"category"]; nspredicate *predicate = [nspredicate predicatewithformat:@"%k == %@", @"categoryid", catid]; [fetchrequest setpredicate:predicate]; // execute fetch request nserror *fetcherror = nil; nsarray *result = [[coredatawrapper mymanagedcontext] executefetchrequest:fetchrequest error:&fetcherror]; if (!fetcherror) { return result[0]; } return nil; }
i'm new coredata , maybe sort of dumb questions, followed many resourses , can't find what's problem.
if goal
object can have 1 category, it's to-one relationship, meaning category
property object, not set of objects (not set of 1 object).
you should set category :
[goal setvalue:objcategory forkey:@"category"];
Comments
Post a Comment