我在iPhone应用程序中设置了父子核心数据关系。我有一个Manufacturer对象和一个Car对象。这与作为所有者的制造商是多对多的关系。主视图是包含制造商的表格视图。详细视图是不同类型汽车的另一个表格视图。我一直在使用Tim Roadley's Core Data Tutorial作为基础。本教程使用Stanford's Core Data Table View Library作为表格视图。核心数据表视图删除崩溃
增加汽车制造商,并给了我没有问题,但是当我去和删除多辆在表视图中我得到这个错误:
*** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-1914.84/UITableView.m:1037
2012-07-29 23:39:33.561应用[16368:c07] *由于未捕获异常'NSInternalInconsistencyException',原因:'无效更新:无效的行数在部分0中。更新后(0)的现有节中包含的行数必须相等到更新之前该部分中包含的行数(2),加上或减去从该部分插入或删除的行数(0插入,1删除)以及正数或负数ows移入或移出该部分(0移入,0移出)。'
如果我删除了唯一的汽车,它工作得很好,直到我尝试添加一个新的车,当我得到这个错误:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Illegal attempt to establish a relationship 'manufacturer' between objects in different contexts (source = <Car: 0x6d96da0> (entity: Car; id: 0x6d8a3c0 <x-coredata:///Car/tC78E17EB-1D68-4998-8C4D-6D1199CE253F4> ; data: {
dateAdded = nil;
manufacturer = nil;
carName = new;
}) , destination = <Manufacturer: 0x6bb1f40> (entity: Manufacturer; id: 0x6d87340 <x-coredata://2E8DDF34-B01A-4203-A53E-73DBE6A2F976/Garden/p6> ; data: <fault>))'
这里是我的编辑方法:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self.tableView beginUpdates];
Plant *plantToDelete = [self.fetchedResultsController objectAtIndexPath:indexPath];
NSLog(@"Deleting plant '%@'", plantToDelete.plantName);
[self.managedObjectContext deleteObject:plantToDelete];
[self.managedObjectContext save:nil];
//delete empty tableview row
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];
NSLog(@"Before performFetch...");
[self performFetch];
NSLog(@"After performFetch...");
[self.tableView endUpdates];
}
}
的performFetch
方法包含在前面提到的CoreDataTableViewController
文件中。为了您的方便,那就是:
(void)performFetch
{
_debug = YES;
if (self.fetchedResultsController) {
if (self.fetchedResultsController.fetchRequest.predicate) {
if (self.debug) NSLog(@"[%@ %@] fetching %@ with predicate: %@", NSStringFromClass([self class]), NSStringFromSelector(_cmd), self.fetchedResultsController.fetchRequest.entityName, self.fetchedResultsController.fetchRequest.predicate);
} else {
if (self.debug) NSLog(@"[%@ %@] fetching all %@ (i.e., no predicate)", NSStringFromClass([self class]), NSStringFromSelector(_cmd), self.fetchedResultsController.fetchRequest.entityName);
}
NSError *error;
[self.fetchedResultsController performFetch:&error];
if (error) NSLog(@"[%@ %@] %@ (%@)", NSStringFromClass([self class]), NSStringFromSelector(_cmd), [error localizedDescription], [error localizedFailureReason]);
} else {
if (self.debug) NSLog(@"[%@ %@] no NSFetchedResultsController (yet?)", NSStringFromClass([self class]), NSStringFromSelector(_cmd));
}
[self.tableView reloadData];
}
根据其他的问题,我用beginUpdates
和endUpdates
正确地这样做。这是一个令人困惑的错误。谢谢你的帮助。
是的,在注释掉该行后,我仍然收到错误。 – Andrew 2012-08-04 21:06:14
我下载了基于你的项目的项目。我看到你在这里的东西几乎与它相同。它在那里工作,虽然我看到几种模式对我来说毫无意义。如果您使用该项目作为基础,则应该查看自己的不同之处。错误消息提到“'非法尝试在不同情况下的对象之间建立关系'制造商'”。你有多个上下文吗?那肯定会做到。 – 2012-08-05 01:28:18