I am having a problem with updating the core data. i am downloading the data in the background thread comparing with the identifier in core data and count but am having trouble to update now i want to check the identifier present in Core data with JSON Response and if id is present in the JSON Response leave it and if not present in JSON Response (That means That record has been removed in Server side)
Here in this code am checking id is present in core data or not and now i want to check the id is present in json or not to update the records
Any help will be great appreciate thanks in advance please check the code how am storing the data in to core data
for (int i = 0; i < [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 this with updating or deleting the records from 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 it ");
}
}
but here the problem is Coredata has 115 records when checking with json but json will have only 114 records and it returns crash