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

 ̄綄美尐妖づ 提交于 2019-12-08 13:35:55

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/18239910/the-fetched-object-at-index-i-has-an-out-of-order-section-name-%c3%a5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!