我利用NSFetchedResultsController来显示一堆对象,这些对象使用日期进行了划分。在全新安装中,这一切都可以正常工作,并且对象将显示在表格 View 中。但是,似乎重新启动该应用程序时会崩溃。我在初始化NSFetchedResultsController时指定了一个缓存,当我不这样做时,它可以完美地工作。
这是我创建NSFetchedResultsController的方法:
- (NSFetchedResultsController *)results {
// If we are not nil, stop here
if (results != nil)
return results;
// Create the fetch request, entity and sort descriptors
NSFetchRequest *fetch = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Event" inManagedObjectContext:self.managedObjectContext];
NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"utc_start" ascending:YES];
NSArray *descriptors = [[NSArray alloc] initWithObjects:descriptor, nil];
// Set properties on the fetch
[fetch setEntity:entity];
[fetch setSortDescriptors:descriptors];
// Create a fresh fetched results controller
NSFetchedResultsController *fetched = [[NSFetchedResultsController alloc] initWithFetchRequest:fetch managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"day" cacheName:@"Events"];
fetched.delegate = self;
self.results = fetched;
// Release objects and return our controller
[fetched release];
[fetch release];
[descriptor release];
[descriptors release];
return results;
}
这些是应用程序崩溃时收到的消息:
FATAL ERROR: The persistent cache of section information does not match the current configuration. You have illegally mutated the NSFetchedResultsController's fetch request, its predicate, or its sort descriptor without either disabling caching or using +deleteCacheWithName:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'FATAL ERROR: The persistent cache of section information does not match the current configuration. You have illegally mutated the NSFetchedResultsController's fetch request, its predicate, or its sort descriptor without either disabling caching or using +deleteCacheWithName:'
我真的不知道为什么这么说,因为我不相信我在做任何会导致这种情况的事情。唯一潜在的问题是节标题(天),我在创建新对象时会像这样构造它:
// Set the new format
[formatter setDateFormat:@"dd MMMM"];
// Set the day of the event
[event setValue:[formatter stringFromDate:[event valueForKey:@"utc_start"]] forKey:@"day"];
就像我提到的,如果不涉及缓存,所有这些都可以正常工作。任何帮助表示赞赏!
最佳答案
苹果发布新的iOS 4.0时,我的一个应用程序遇到了类似的问题。
搜索:
fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:[self managedObjectContext] sectionNameKeyPath:nil cacheName:nil];
并将参数cacheName的值设置为nil。它对我有用,希望对您有用。让我知道。
关于iphone - NSFetchedResultsController在performFetch上崩溃:使用缓存时,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2709768/