NSFetchRequest 控制器在 iOS 5 上工作,在 iOS 4.3 上崩溃
Posted
技术标签:
【中文标题】NSFetchRequest 控制器在 iOS 5 上工作,在 iOS 4.3 上崩溃【英文标题】:NSFetchRequestController works on iOS5, crash on iOS4.3 【发布时间】: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)。但只有在第一次重新启动之后。第一次启动还是出错...
【问题讨论】:
【参考方案1】:您使用的是章节索引标题吗?这似乎是这个错误所指的。
只需添加:
-(NSString *)controller:(NSFetchedResultsController *)controller
sectionIndexTitleForSectionName:(NSString *)sectionName
return sectionName;
到您的表格视图控制器。
【讨论】:
这实际上并没有解决 localCaseInsensitiveCompare 的问题,但它确实解决了 caseInsensitiveCompare 的错误顺序。所以这是一场胜利!由于我的应用程序仅在瑞典语中本地化,它似乎可以自行解决。谢谢。以上是关于NSFetchRequest 控制器在 iOS 5 上工作,在 iOS 4.3 上崩溃的主要内容,如果未能解决你的问题,请参考以下文章
NSFetchRequest 谓词不一致 iOS (NSFetchedResultsController) 与 Mac (NSArrayController)
iOS - NSFetchRequest:对字符串长度进行排序