大家好,所以我的 NSFetchedResultsController 在 3.1 SDK 下工作正常,但是我开始遇到一些奇怪的错误,特别是当我在 3.0 下尝试时在委托方法中。我已经确定这与 NSFetchedResultsControllerDelegate 方法有关。这就是我设置的。
inEditingMode 的内容与我实现向表中添加另一个静态部分的方式有关。
- (void)controllerWillChangeContent:(NSFetchedResultsController*)controller {
[self.tableView beginUpdates];
}
- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type{
NSIndexSet *sectionSet = [NSIndexSet indexSetWithIndex:sectionIndex];
if(self.inEditingMode){
sectionSet = [NSIndexSet indexSetWithIndex:sectionIndex + 1];
}
switch (type) {
case NSFetchedResultsChangeInsert:
[self.tableView insertSections:sectionSet withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[self.tableView deleteSections:sectionSet withRowAnimation:UITableViewRowAnimationFade];
break;
default:
[self.tableView reloadData];
break;
}
}
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath{
NSIndexPath *relativeIndexPath = indexPath;
NSIndexPath *relativeNewIndexPath = newIndexPath;
if(self.inEditingMode){
relativeIndexPath = [NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section + 1];
relativeNewIndexPath = [NSIndexPath indexPathForRow:newIndexPath.row inSection:newIndexPath.section + 1];
}
switch(type) {
case NSFetchedResultsChangeInsert:
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:relativeNewIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:relativeIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
default:
[self.tableView reloadData];
break;
}
}
-(void)controllerDidChangeContent:(NSFetchedResultsController *)controller{
[self.tableView endUpdates];
}
当我将实体添加到托管对象上下文时,我收到以下错误:
Serious application error. Exception was caught during Core Data change processing: *** -[NSCFArray objectAtIndex:]: index (1) beyond bounds (1) with userInfo (null)
我在 objc_exception_throw 上放了一个断点,崩溃似乎发生在 controllerDidChangeContent 内部。
如果我注释掉所有 self.tableView 方法,并在 controllerDidChangeContent 中放置一个 [self.tableView reloadData],一切都会按预期工作。
有人知道为什么会这样吗?