I am quite familiar with regular usage patterns of Core Data, but recently stumbled upon a problem: Imagine a simple case where I have an Entity of person, with 2 string attributes name and company. I want to make a UITableView sorted by names and divided into sections by company name. Sound simple enough:
...
personFetchController_ = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:mainDataCenter.managedObjectContext
sectionNameKeyPath:@"company"
cacheName:@"PersonListCache"];
NSError *error = nil;
if (![personFetchController_ performFetch:&error])
{
LogError(@"Error fetching persons: %@", [error localizedDescription]);
}
personFetchController_.delegate = self;
I register as delegate to listen to changes, especially changes to sections:
- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type
{
NSIndexSet *sections = [NSIndexSet indexSetWithIndex:sectionIndex];
switch (type)
{
case NSFetchedResultsChangeInsert:
[personTableView_ insertSections:sections withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[personTableView_ deleteSections:sections withRowAnimation:UITableViewRowAnimationFade];
default:
break;
}
}
It works very well when I add / remove / change the name of a person, but if I change a person's company name (which means to move from one section to another), the app crashes, saying after an insert, the number of rows in the section needs to be the old value plus one.
Anyone got this working right?