I am having problems with the context in Core Data not being able to save.
I get random crashes when I try to call [context save:]. Sometimes it works, sometimes it doesn't and crashes the app. Here is my delete code. I have been able to reduce the number of crashes by checking if [context respondsToSelector] save. The strange this is even when it fails (respondsToSelector fails), and I didn't call save, it still gets deleted!? But also when the respondsToSelector succeeds, and I attempt to call save, it still crashes sometimes. So the code is a little more stable with the test, but I think there is something wrong with Core Data and the save method. It has been very hard to track down the problem because it really does seem random.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the managed object.
NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
Accidents* accidentDelete = [self.fetchedResultsController objectAtIndexPath:indexPath];
[context deleteObject:accidentDelete];
// Causing crash...
NSError *error = nil;
if ([context respondsToSelector:@selector(save:)])
if (![context save:&error]) {
// Update to handle the error appropriately.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
exit(-1); // Fail
}
else
NSLog(@"Error! Context does not respond to save!");
}
}