ios - Adding a relationship in core data -
i have been @ single task several days trying relationships between core data entities working. have achieved need change new attribute value has relationship added existing object. 1 - - many database.
i not sure how add relationship object exists. in new object getting added routinedetail, how create relationship object exists in routine entity?
i have looked @ several examples showing how add relationships newly added objects need new object in routinesdetails
has relationship value exists in routines
.
the value of routines
held in string called routinetext
rout nsmangedobject entity routines routdet nsmanagedobject entity routinesdetails
i have left commented out code allows me add relationship when both new objects created.
this last thing have in project driving me insane. eternally grateful fix here. advice appreciated best knowledge portal there is. thank you.
nsmanagedobjectcontext *context = [self managedobjectcontext]; // create new device excerciseinfo *info = [_fetchedresultscontroller objectatindexpath:indexpath]; //rout = [nsentitydescription insertnewobjectforentityforname:@"routines" inmanagedobjectcontext:context]; routdet = [nsentitydescription insertnewobjectforentityforname:@"routinesdetails" inmanagedobjectcontext:context]; //add attribute values //[rout setvalue: routinetext forkey:@"routinename"]; [routdet setvalue: info.name forkey:@"image"]; //create relationship [rout addroutinedetobject:routdet];
your main problem statement is, think, here:
i need new object in routinesdetails has relationship value exists in routines.
i presume data model looks this:
routine <----> routinedetail
i.e. every 1 routine has 1 routine detail (one-to-one relationship).
but not make sense. include attributes of routinedetail in routine entity.
instead of
desiredvalue = routinedetail.routine.value;
you have
desiredvalue = routinedetail.value;
also, please note code has number of problems. first line unnecessary, use self.managedobjectcontext
. additionally, against convention using capital initials variables. should reserved class names. method add relationship not right.
you can add relationship this, without method call:
routineobject.detail = detailobject;
Comments
Post a Comment