为什么在将if statements
添加到NSPredicate
后会产生缓存错误?在我尝试根据UITABLEVIEW的标题过滤结果后,错误到了。
我收到以下错误:
CoreData: 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
我查看了其他类似的错误,但无法将它们与我的代码联系起来。
- (NSFetchedResultsController *)fetchedResultsController {
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"ExcerciseInfo" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
NSSortDescriptor *sort = [[NSSortDescriptor alloc]
initWithKey:@"details.muscle" ascending:NO];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];
[fetchRequest setFetchBatchSize:20];
// Sort Results
if ([self.title isEqual: @"Abdominals"]) {
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"details.muscle == %@", @"Back"];
[fetchRequest setPredicate:predicate];
}
else if ([self.title isEqual: @"Arms"]) {
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"details.muscle == %@", @"Biceps"];
[fetchRequest setPredicate:predicate ];
}
NSFetchedResultsController *theFetchedResultsController =
[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:managedObjectContext sectionNameKeyPath:nil
cacheName:@"Root"];
self.fetchedResultsController = theFetchedResultsController;
_fetchedResultsController.delegate = self;
return _fetchedResultsController;
}
答案 0 :(得分:3)
离开视图时,需要在tableViewController:
中 -(void)viewDidDisappear:(BOOL)animated{
[super viewDidDisappear:animated];
[NSFetchedResultsController deleteCacheWithName:@"Root"];
self.fetchedResultsController = nil;
}
还有:
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
[NSFetchedResultsController deleteCacheWithName:@"Root"];
}
return self;
}
或最好的形式在这里(全部在一起):
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
[NSFetchedResultsController deleteCacheWithName:@"Root"];
self.fetchedResultsController = nil;
}
return self;
}
答案 1 :(得分:1)
基本上,如果您使用带有获取结果控制器的cacheName
,则不应更改获取请求。
如果您确实更改了获取请求,则必须删除缓存,否则您将收到与缓存内容相关的错误,这些错误不再与您获取的结果相匹配。
如果您想更改获取请求,通常不会从缓存中获得任何好处。