NSFetchRequestController适用于iOS5,在iOS4.3上崩溃

时间:2011-12-23 13:33:20

标签: objective-c core-data ios4 nsfetchedresultscontroller

我一直在尝试在需要最低iOS 4.3的项目中实施Core Data。我让代码在iOS 5上没有任何问题,但是在使用iOS 4.3时,它崩溃的原因如下:

Unresolved error Error Domain=NSCocoaErrorDomain Code=134060 "The operation couldn’t be completed. (Cocoa error 134060.)" UserInfo=0x4fb59b0 {reason=The fetched object at index 4 has an out of order section name 'Å. Objects must be sorted by section name'}, {
reason = "The fetched object at index 4 has an out of order section name '\U00c5. Objects must be sorted by section name'";

这是我的代码:

- (NSFetchedResultsController *)fetchedResultsController
{
if (__fetchedResultsController != nil) {
    return __fetchedResultsController;
}

// Set up the fetched results controller.
// Create the fetch request for the entity.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.

fetchRequest.entity = [NSEntityDescription entityForName:@"Exhibitor" 
                                  inManagedObjectContext:self.managedObjectContext];

// Edit the sort key as appropriate.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" 
                                                               ascending:YES 
                                                                selector:@selector(caseInsensitiveCompare:)];

fetchRequest.sortDescriptors = [NSArray arrayWithObject:sortDescriptor];

// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] 
                                                         initWithFetchRequest:fetchRequest
                                                         managedObjectContext:self.managedObjectContext 
                                                         sectionNameKeyPath:@"firstLetter" 
                                                         cacheName:nil];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;

NSError *error = nil;
if (![self.fetchedResultsController performFetch:&error]) {
    /*
     Replace this implementation with code to handle the error appropriately.

     abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. 
     */
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
}

  return __fetchedResultsController;
}   

如果我在sortDesctriptor中选择使用caseInsensitivecompare:而不是localizedCaseInsensitiveCompare:它不会崩溃但顺序错误(因为我需要ÅÄÖ在底部,而不是在A和O之后)。

建议?

更新 好像当我在多任务栏中杀死我的应用程序然后重新启动它时,ÅÄÖ的顺序是正确的(使用caseInsensitiveCompare)。但只有在第一次重新启动后。首次发布时仍然是错误的......

1 个答案:

答案 0 :(得分:1)

您使用的是分区索引标题吗?这似乎是这个错误所指的。

只需添加:

-(NSString *)controller:(NSFetchedResultsController *)controller
             sectionIndexTitleForSectionName:(NSString *)sectionName {
   return sectionName;
}

到你的表视图控制器。