iphone - Assigning values to an object in core data -
below code snippet:
nsfetchrequest *request= [[nsfetchrequest alloc] init]; nsentitydescription *entity = [nsentitydescription entityforname:@"login" inmanagedobjectcontext:self.managedobjectcontext]; nspredicate *predicate =[nspredicate predicatewithformat:@"userid==%@ , password==%@",useridentered, passwordentered]; [request setentity:entity]; [request setpredicate:predicate]; nserror *anyerror=nil; nsarray *fetchedobjects = [self.managedobjectcontext executefetchrequest:request error:&anyerror]; login *login = [fetchedobjects objectatindex:0]; information *information = [nsentitydescription insertnewobjectforentityforname:@"information" inmanagedobjectcontext:self.managedobjectcontext]; login.information = information; login.information.title = informationtitletextview.text; login.information.info_1 = information1textview.text; login.information.info_2 = information2textview.text; [self.managedobjectcontext save:nil]; // write database nslog(@"login.information %@",login.information.title); nslog(@"login.title %@", login.userid); [self.delegate savebuttontapped:self];
the problem is, works fine first time. when again add information same user overwrites existing one. how make sure gets written next line?
you creating new managed object information (new db row):
information *information = [nsentitydescription insertnewobjectforentityforname:... inmanagedobjectcontext:...];
and assign user.
login.information = information;
this not remove old information db, user loses relationship it.
i suppose want having more 1 information
instances connected 1 login
instance. means have open managed object model , change one-to-one relationship one-to-many relationship.
then code be:
[login.informations addobject:information];
or
information.login = login;
Comments
Post a Comment