I still have difficulties to understand how core data works in background thread even after having read lots of things about it, especially for deleting objects.
As an example, if I want to delete an object from a context like this :
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSManagedObjectContext *context = [self managedObjectContext];
if (editingStyle == UITableViewCellEditingStyleDelete)
{
// Delete object from database
[context deleteObject:[self.tests objectAtIndex:indexPath.row]];
NSError *error = nil;
if (![context save:&error]) {
NSLog(@"Can't Delete! %@ %@", error, [error localizedDescription]);
return;
}
[self.tests removeObjectAtIndex:indexPath.row];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
This works, but when the data is big, [context save:&error]
takes a lot of time, so how can I do it in background ?
It seems that I can't work with another context, otherwise I get the error an nsmanagedobjectcontext cannot delete objects in other contexts
.
I have tried hundreds of different things, but I'm lost...
Thanks !