Search code examples
objective-ccore-dataios6nsfetchedresultscontroller

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


Error fetching: Error Domain=NSCocoaErrorDomain Code=134060 

"The operation couldn’t be completed. (Cocoa error 134060.)" 
UserInfo=0x132eb960 {reason=The fetched object at index 76 
has an out of order section name 'å. Objects must be sorted by section name'}

I checked and there are other questions with the same name, however in this case the problem appears due to diacritic alphabet symbols.

- (NSFetchedResultsController *)fetchedResultsControllerWithPredicate:
(NSPredicate *)aPredicate {
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSManagedObjectContext *localContext = [NSManagedObjectContext MR_contextForCurrentThread];

    fetchRequest.entity = [Word MR_entityDescription];

    [fetchRequest setFetchBatchSize:20];
    [fetchRequest setPredicate:aPredicate];

    NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"name"
                                                                     ascending:YES
                                                                      selector:@selector(localizedCompare:)];

    fetchRequest.sortDescriptors = @[sortDescriptor];

    NSFetchedResultsController *aFetchedResultsController =
    [[NSFetchedResultsController alloc]
     initWithFetchRequest:fetchRequest
     managedObjectContext:localContext
     sectionNameKeyPath:@"name.stringGroupByFirstInitial" cacheName:nil];

    aFetchedResultsController.delegate = self;

    NSError *anyError = nil;
    if (![aFetchedResultsController performFetch:&anyError]) {
        NSLog(@"Error fetching: %@", anyError);
    }

    return aFetchedResultsController;
}

If I replace localizedCompare: with compare: then there is no fetch error, however some indexes are in the wrong order, and there are no section shown.


Solution

  • Not a solution per se, unfortunately...

    localizedCompare: does not return the same results as what is returned from a fetch request to a SQLite backed persistent store using its own implementation of a localized compare. This is particularly evident when comes to "unusual" characters.

    The character Æ is another one that throws a wrench into the system, as do many Icelandic and Scandinavian language characters. I have been banging my head against this issue for two years to no avail.

    Otherwise, it will work great as long as you don't have these "unusual" characters present. My workaround was to detect the error, set sectionNameKeyPath to nil, then force a re-fetch. Not ideal, nor efficient, but better than the user seeing nothing.