2013-06-27 76 views
1

爲什麼在將if statements添加到NSPredicate之後會產生緩存錯誤?我嘗試根據UITABLEVIEW的標題過濾結果後,錯誤到達。Core Data Cache Error with NSPredicate

我得到以下錯誤:

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; 

} 

回答

1

基本上,如果你使用cacheName與讀取的結果控制器,你不應該改變的讀取請求。

如果確實需要更改提取請求,則必須刪除緩存,否則會得到與緩存內容相關的錯誤不再與您提取的結果相匹配。

如果您想更改提取請求,您通常不會從擁有緩存中獲益。

+0

非常簡單和容易。我現在看到了。我將緩存更改爲零,並修復了錯誤。我遇到的唯一問題是針對if語句。 if與'self.title isEqual:@'Arms''的標題相關,但不起作用。任何快速的想法? – Sgillon

+0

正如你所看到的我接受你的anwser :) – Sgillon

+0

'isEqual'應該工作,假設文本實際上匹配。 'isEqualToString'更高效。記錄標題是什麼,它需要完全匹配。 – Wain

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; 
}