Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Core Data error: 'Objects must be sorted by section name' for specific languages such as Thai

CoreData: error: (NSFetchedResultsController) The fetched object at index 72 has an out of order section name 'อั. Objects must be sorted by section name'

I am using the following code to sort by book title field and display book title's first upper case letter as the section name in a UITableViewController.

The code runs perfectly in all languages except of Thai. I read on the Internet that there are special non US characters causing such issues (i.e. Æ), but I didn't find any solution yet.

See gschandler response on The fetched object at index [i] has an out of order section name 'å

Code of FRC is

 NSFetchedResultsController *aFetchedResultsController = 
    [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                managedObjectContext:managedObjectContext
                                  sectionNameKeyPath:@"titleFirstLetter"
                                           cacheName:nil];

Code of firstLetter is:

- (NSString *)titleFirstLetter {

    // support UTF-16:
    NSString* val = [self.title substringWithRange:[self.title rangeOfComposedCharacterSequenceAtIndex:0]];

    return [val uppercaseString];
}

Any suggestions?

like image 254
Joshua Avatar asked Apr 29 '14 10:04

Joshua


1 Answers

You have to add a sort descriptor that sorts on the same property the sectionNameKeyPath does.

NSSortDescriptor *sort = [[NSSortDescriptor alloc]initWithKey:@"titleFirstLetter" ascending:NO];   
[fetchRequest setSortDescriptors:[NSArray arrayWithObjects:sort,nil]];
[fetchRequest setFetchBatchSize:20];
like image 180
BossBols Avatar answered Sep 20 '22 03:09

BossBols