ios - Update Coredata Attributes without Duplication -
i have 3 textfields-name,age,phone number every time entering values on each fields stored on core data
also want check repetition in db, is
- first want check there repetition in name
- if repetition, check age , phone number.
- if age same can not change.
- if difference in phone number, second number replace first phone number.
i tried lot.. pls me
my code is
nsentitydescription *entitydescc=[nsentitydescription entityforname:@"name" inmanagedobjectcontext:self.managedobjectcontext]; nsfetchrequest *request=[[nsfetchrequest alloc]init]; [request setentity:entitydescc]; nspredicate *predicate=[nspredicate predicatewithformat:@"sname %@ ",self.nametextfield.text]; [request setpredicate:predicate]; nserror *error; nsarray *matchingdata=[self.managedobjectcontext executefetchrequest:request error:&error]; if (matchingdata.count<=0) { nsentitydescription *entitydesc=[nsentitydescription entityforname:@"name" inmanagedobjectcontext:self.managedobjectcontext]; nsmanagedobject *newobject=[[nsmanagedobject alloc]initwithentity:entitydesc insertintomanagedobjectcontext:self.managedobjectcontext]; [newobject setvalue:self.nametextfield.text forkey:@"sname"]; [newobject setvalue:self.agetextfield.text forkey:@"age"]; nserror *error; [self.managedobjectcontext save:&error]; } else{ int count=0; (record *obj in matchingdata) { nsentitydescription *entitydescc=[nsentitydescription entityforname:@"name" inmanagedobjectcontext:self.managedobjectcontext]; nsfetchrequest *request=[[nsfetchrequest alloc]init]; [request setentity:entitydescc]; nspredicate *predicate=[nspredicate predicatewithformat:@"age %@ ",self.agetextfield.text]; [request setpredicate:predicate]; [request setpredicate:predicate]; nserror *error; nsarray *matchingdata=[self.managedobjectcontext executefetchrequest:request error:&error]; if (matchingdata.count<=0) { nslog(@"entered"); nsentitydescription *entitydesc=[nsentitydescription entityforname:@"name" inmanagedobjectcontext:self.managedobjectcontext]; nsmanagedobject *newobject=[[nsmanagedobject alloc]initwithentity:entitydesc insertintomanagedobjectcontext:self.managedobjectcontext]; [newobject setvalue:self.agetextfield.text forkey:@"age"]; nserror *error; [self.managedobjectcontext save:&error]; } ++count; } }
it not check attribute age
save 3 details in single entity. fetch records in given entity, out predicate. create model class each record. business logic using model class list. solve problem , make app perform better.
Comments
Post a Comment