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开发者_C百科 driving me nuts and I just can't figure it out. Any help would be greatly appreciated!
This seems to be a bug in Apple's code. I had some success using this SafeFetchedResultsController subclass.
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.
精彩评论