【发布时间】:2011-12-23 13:33:19
【问题描述】:
我一直在尝试在一个至少需要 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;
}
如果我在我的 sortDescriptor 中选择使用 caseInsensitivecompare: 而不是localizedCaseInsensitiveCompare: 它不会崩溃但顺序错误(因为我需要 Å Ä Ö 位于底部,而不是在 A 和 O 之后)。
建议?
更新: 好像当我在多任务栏中杀死我的应用程序然后重新启动它时,ÅÄÖ 的顺序是正确的(使用 caseInsensitiveCompare)。但只有在第一次重新启动之后。第一次启动还是出错...
【问题讨论】:
标签: objective-c core-data ios4 nsfetchedresultscontroller