i having problem updating core data. downloading data in background thread comparing identifier in core data , count having trouble update want check identifier present in core data json response , if id present in json response leave , if not present in json response (that means record has been removed in server side)
here in code checking id present in core data or not , want check id present in json or not update records
any great appreciate in advance please check code how storing data in core data
for (int = 0; < [arraydata count]; i++) { nsnumber * idnum = [arraydata objectatindex:i][@"id"]; nsfetchrequest *request = [nsfetchrequest fetchrequestwithentityname:@"deal"]; [request setpredicate:[nspredicate predicatewithformat:@"identifier == %@",idnum]]; [request setfetchlimit:1]; nsuinteger count = [_managedobjectcontext countforfetchrequest:request error:&error]; if (count == nsnotfound) { nslog(@"error"); }else if (count == 0) { deal * dealsentity = [nsentitydescription insertnewobjectforentityforname:@"deal" inmanagedobjectcontext:_managedobjectcontext]; nsstring * name = [arraydata objectatindex:i][@"name"]; dealsentity.nameattribute = name; dealsentity.identifier = idnum; [appdelegate savecontext]; } [self performselectoronmainthread:@selector(updatedata:) withobject:_myarray waituntildone:yes]; } - (void) updatedata:(nsarray *)yourdata { nsfetchrequest *fetchrequest = [[nsfetchrequest alloc]init]; nsentitydescription *entity = [nsentitydescription entityforname:@"deal" inmanagedobjectcontext:_managedobjectcontext]; [fetchrequest setreturnsobjectsasfaults:no]; [fetchrequest setentity:entity]; nserror *error; yourdata = [_managedobjectcontext executefetchrequest:fetchrequest error:&error]; self.myarray = yourdata; [listtableview reloaddata]; }
i have tried updating or deleting records coredata
for (int d = 0; d < [_myarray count]; d++) { deal * deal = (deal*)_myarray[d]; nsnumber * identifier = [deal identifier]; if ([identifier isequaltonumber:[[arraydata objectatindex:d] valueforkey:@"id"]] ) { nslog(@"equal %d",d); } else { nslog(@"kill "); } }
but here problem coredata has 115 records when checking json json have 114 records , returns crash
as have identifier database need change code little bit
nsfetchrequest *request = [[nsfetchrequest alloc] init]; [request setentity:[nsentitydescription entityforname:@"deal" inmanagedobjectcontext:moc]]; [request setpredicate:[nspredicate predicatewithformat:@"identifier == %@",idnum]]; [request setfetchlimit:1]; nserror *error = nil; nsarray *results = [moc executefetchrequest:request error:&error]; // check count if([results count] == 1) { // update coredata object } else { // create new object }
Comments
Post a Comment