I’m setting an NSFetchedResultsController with the sort descriptor
sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@”name” ascending:YES selector:@selector(caseInsensitiveCompare:)];
And the section
aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@”name” cacheName:nil];
Then I also set the table view section’s delegate like this:
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return [self.fetchedController sectionIndexTitles];
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
return [self.fetchedController sectionForSectionIndexTitle:title atIndex:index];
}
Sometimes, when I insert objects, I’m getting the error “The fetched object at index X has an out of order section name ‘something. Objects must be sorted by section name'”;
I’m inserting thousands of rows. When the error happens, it’s always with the same case: I have 10 objects called the same thing, but some are upper case and others are lower case. Example: nameexample, nameExample, nameEXAMPLE, nameexample, nameExample, etc.
Any idea why do I get the error and how can I fix this?
The reason why I have it case insensitive is because my sections are based on the first letter and if I don’t have it case insensitive I’ll get section ‘a’ and section ‘A’.
I think the order of the section is different from the order of the sort because although I’m using the same attribute, one is case insensitive and the other isn’t.
Solution: I’ve added another attribute in the Core Data which contains the name in lower case and I use this attribute in the section and sort. This way I don’t have to call @selector(caseInsensitiveCompare:)