即使在阅读了很多关于它的内容之后,我仍然很难理解核心数据在后台线程中的工作原理,尤其是对于删除对象。
例如,如果我想从这样的上下文中删除一个对象:
- (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];
}
}
这行得通,但是当数据很大时,[context save:&error]
会花费很多时间,那么我该如何在后台进行呢?似乎我无法使用其他上下文,否则我会收到错误消息an nsmanagedobjectcontext cannot delete objects in other contexts
。我已经尝试了数百种不同的东西,但我迷路了......谢谢!