严重的应用程序错误
我有一个 TableView 控制器类,它使用获取的结果控制器来显示从核心数据模型获取的“患者”实体列表。该表的各个部分取自称为“位置”的患者属性。这是获取请求的排序描述符:
NSSortDescriptor *locationDescriptor = [[NSSortDescriptor alloc] initWithKey:@"location" ascending:YES];
NSSortDescriptor *lastNameDescriptor = [[NSSortDescriptor alloc] initWithKey:@"lastName" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:locationDescriptor, lastNameDescriptor, nil];
这是 FRC 的初始化代码:
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:@"location" cacheName:@"List"];
当我想添加新的“患者”实体时 - 我单击添加按钮,然后将“添加新患者”视图控制器推送到导航堆。
我添加的第一个患者效果很好。
如果我添加第二个患者 - 应用程序有时会崩溃并出现以下错误:
2010-03-22 14:42:05.270 患者[1126:207] 严重的应用错误。核心数据更改处理期间捕获异常:* -[NSCFArray insertObject:atIndex:]:索引 (1) 超出边界 (1) with userInfo (null) 2010-03-22 14:42:05.272 患者[1126:207] * 由于未捕获的异常“NSRangeException”而终止应用程序,原因:“*** -[NSCFArray insertObject:atIndex:]:索引(1)超出界限 (1)'
这似乎仅在患者添加了位置时才会发生(如果未添加位置,则位置默认为“未知”)。这似乎也与位置的排序有关。例如,如果第一个患者位置 = 14 号病房,第二个 = 9 号病房,那么它肯定会崩溃。
我想知道这是否与我要求获取的结果控制器对部分名称进行排序的方式有关?
这个错误让我抓狂,我就是想不通。任何帮助将不胜感激!
I have a TableView controller class that uses a fetched results controller to display a list of 'patient' entities taken from a Core Data model. The sections of this table are taken from a patient attribute called 'location'. Here is the sort descriptor for the fetch request:
NSSortDescriptor *locationDescriptor = [[NSSortDescriptor alloc] initWithKey:@"location" ascending:YES];
NSSortDescriptor *lastNameDescriptor = [[NSSortDescriptor alloc] initWithKey:@"lastName" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:locationDescriptor, lastNameDescriptor, nil];
Here is the initialisation code for the FRC:
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:@"location" cacheName:@"List"];
When I want to add a new 'patient' entity - I click an add button which then pushes an 'add new patient' view controller to the navigation stack.
The first patient I add works fine.
If I add a second patient - the app will sometimes crash with the following error:
2010-03-22 14:42:05.270 Patients[1126:207] Serious application error. Exception was caught during Core Data change processing: * -[NSCFArray insertObject:atIndex:]: index (1) beyond bounds (1) with userInfo (null)
2010-03-22 14:42:05.272 Patients[1126:207] * Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSCFArray insertObject:atIndex:]: index (1) beyond bounds (1)'
This only seems to happen if the patient's have a location added (if none is added then the location defaults to 'unknown'). It seems to have something to do with the sorting of the location too. For instance, if the first patient location = ward 14 and the second = ward 9 then it crashes without fail.
I'm wondering if this is something to do with how I am asking the fetched results controller to sort the section names??
This bug is driving me nuts and I just can't figure it out. Any help would be greatly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这似乎是苹果代码中的一个错误。我使用 这个 SafeFetchedResultsController 子类取得了一些成功。
This seems to be a bug in Apple's code. I had some success using this SafeFetchedResultsController subclass.
每当您在使用 NSFetchedResultsController 时看到“在核心数据更改处理期间捕获异常”消息时,您应该立即开始查看您的 NSFetchedResultsControllerDelegate 方法。我建议在控制器的开头设置一个断点:didChangeObject:atIndexPath:forChangeType:newIndexPath 方法。然后逐步执行此方法并观察崩溃发生的位置。问题可能是您没有在此方法中正确管理节插入和删除。
Anytime you see the message "Exception was caught during Core Data change processing" when using NSFetchedResultsController, you should immediately begin to look at your NSFetchedResultsControllerDelegate methods. I recommend setting a breakpoint at the beginning of the controller:didChangeObject:atIndexPath:forChangeType:newIndexPath method. Then step through this method and observe where the crash is occurring. The problem may be that you are not managing your section insertion and deletions correctly in this method.