2012-10-22 74 views
1

我有以下代碼:NSFetchResultsController委託不獲取調用

- (NSFetchedResultsController *)fetchedResultsController { 
    // Set up the fetched results controller if needed. 
    if (fetchedResultsController == nil) { 

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
     // Edit the entity name as appropriate. 
     NSEntityDescription *entity = [NSEntityDescription entityForName:@"DiskStory" inManagedObjectContext:self.managedObjectContext]; 
     [fetchRequest setEntity:entity]; 

     fetchRequest.predicate = [NSPredicate predicateWithFormat:@"isRemoved == %@", [NSNumber numberWithBool:NO]]; 

     // Edit the sort key as appropriate. 
     NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"created" ascending:NO]; 
     NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil]; 

     [fetchRequest setSortDescriptors:sortDescriptors]; 

     // Set limit 
     [fetchRequest setFetchBatchSize:25]; 
     // Set batch size 
     [fetchRequest setFetchLimit:50]; 


     NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:kSavedStoryCache]; 
     aFetchedResultsController.delegate = self; 
     fetchedResultsController = aFetchedResultsController; 
} 
    return fetchedResultsController; 
}  
在我viewDidLoad中

我:

NSError *error = nil; 
    if (![[self fetchedResultsController] performFetch:&error]) { 
     NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
    } 

我想知道爲什麼我的:

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller { 

} 

委託,ISN不叫?我在裏面放了一個斷點。任何想法?

+0

你在' - (NSFetchedResultsController *)fetchedResultsController'中放置了一個斷點嗎?你確定你的初始化正在運行嗎? – Don

+0

是的,我在那裏放了一個斷點,它會一直持續到 – adit

+0

並且你解決了這個問題嗎? –

回答

0

我想你已經使用了自我。在錯誤的地方。請參閱下面的代碼;我已經添加了註釋的修改

- (NSFetchedResultsController *)fetchedResultsController { 
    // Set up the fetched results controller if needed. 
    if (fetchedResultsController == nil) { 

     NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
     // Edit the entity name as appropriate. 
     NSEntityDescription *entity = [NSEntityDescription entityForName:@"DiskStory" inManagedObjectContext:self.managedObjectContext]; 
     [fetchRequest setEntity:entity]; 

     fetchRequest.predicate = [NSPredicate predicateWithFormat:@"isRemoved == %@", [NSNumber numberWithBool:NO]]; 

     // Edit the sort key as appropriate. 
     NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"created" ascending:NO]; 
     NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil]; 

     [fetchRequest setSortDescriptors:sortDescriptors]; 

     // Set limit 
     [fetchRequest setFetchBatchSize:25]; 
     // Set batch size 
     [fetchRequest setFetchLimit:50]; 


     NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:kSavedStoryCache]; 
     aFetchedResultsController.delegate = self; 
     fetchedResultsController = aFetchedResultsController;  // -> Shouldn't be self.fetchedResultsController 
} 
    return fetchedResultsController;  // -> Shouldn't be self.fetchedResultsController 
} 

另外,如果您有合成的fetchedResultsController如下:

fetchedResultsController = _fetchedResultsController 

然後在- (NSFetchedResultsController *)fetchedResultsController方法,你應該把它稱爲_fetchedResultsController

+0

我已經根據你的修改改變了它,但它沒有幫助..委託仍然沒有被調用。我在執行fetchRequest後放置了一個斷點,它不是零 – adit

+0

我也將它合成爲fetchedResultsController – adit

+0

提取多少節或記錄? – user427969

2

我有此問題之前,它不會在您首次撥打performFetch時調用,但僅當fetchedResultsController中的數據實際發生更改時纔會調用。例如,當您從該提取結果中的上下文中刪除對象時,將調用委託。

+0

無賴......所以我該如何解決這個問題?我用於表視圖數據源的數據得到錯誤的狀態..因爲我需要調用reloadData後取回完成 – adit

+0

我通過發佈通知後初始提取完成解決它。然後在我的'UIViewController'中我重新加載了我的組件。 –

+0

您怎麼知道初始抓取完成了? – adit