我自己无法重现这一点,所以我只需要收集一些我收集的错误日志即可。这个实在想不通。崩溃发生时有 2 个 FetchedResults 控制器处于活动状态。一个在主屏幕上显示每个类别中有多少项目,一个用于处理您深入研究的类别。我通过其余的回溯知道的是,在向下钻取视图中插入对象时总是会发生此问题。有任何想法吗?有人至少可以确认问题在于更新主屏幕上的单元格而不是在详细屏幕上插入单元格吗?
错误:
** 请注意,有趣的部分似乎是范围 {0,3} 和边界 [0 .. 1] 在我收到的有关此问题的 5-10 条日志中始终相同 **
应用程序特定信息: * 由于未捕获的异常而终止应用程序 \'NSRangeException\',原因:\'* -[NSArray indexOfObject:inRange:]: range {0, 3} extends beyond bounds [0 .. 1]\'
Last Exception Backtrace: 0 CoreFoundation 0x35ad988f __exceptionPreprocess + 163
1 libobjc.A.dylib 0x336fd259 objc_exception_throw + 33
2 CoreFoundation 0x35acda09 -[NSArray indexOfObject:inRange:] + 205
3 CoreData 0x32e8e73d -[_NSDefaultSectionInfo indexOfObject:] + 133
4 CoreData 0x32e8f299 -[NSFetchedResultsController indexPathForObject:] + 149
5 CoreData 0x32e956f5 -[NSFetchedResultsController(PrivateMethods) _managedObjectContextDidChange:] + 4361
创建控制器:
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:mainContext sectionNameKeyPath:nil cacheName:self.groupID];
NSFetchedResultsController 代码:
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath
{
switch(type) {
case NSFetchedResultsChangeInsert:
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeUpdate:
[self configureCell:[self.tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
break;
case NSFetchedResultsChangeMove:
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
[self.tableView endUpdates];
}
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller
{
[self.tableView beginUpdates];
}
配置单元格:
-(void)configureCell:(MessageRow *)cell atIndexPath:(NSIndexPath *)indexPath {
MymanagedObject *myobj = [self.convoController objectAtIndexPath:indexPath];
cell.title = myobj.title
}
请注意,在我的主屏幕上,所有操作基本上都以相同的方式完成,除了在更新时重新加载 tableview 行而不是配置
case NSFetchedResultsChangeUpdate:
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
break;