我不明白爲什麼,在我的生活中,當我向底層數據存儲添加數據時,NSFetchedResultsControllerDelegate方法沒有觸發。如果我重新啓動iPhone應用程序,數據立即顯示。NSFetchedResultsControllerDelegate沒有觸發
我有子類的UITableViewController,符合NSFetchedResultsControllerDelegate:
@interface ProjectListViewController : UITableViewController <NSFetchedResultsControllerDelegate> {
NSFetchedResultsController* fetchedResultsController_;
NSManagedObjectContext* managedObjectContext_;
}
我實例化NSFetchedResultsController和委託設置爲self:
// Controller
fetchedResultsController_ = [[NSFetchedResultsController alloc] initWithFetchRequest:request
managedObjectContext:self.managedObjectContext
sectionNameKeyPath:@"Client"
cacheName:@"ProjectsCache"];
fetchedResultsController_.delegate = self;
我實現委託方法:
- (void)controllerWillChangeContent:(NSFetchedResultsController*)controller {
NSLog(@"ProjectListViewController.controllerWillChangeContent");
// The fetch controller is about to start sending change notifications, so prepare the table view for updates.
[self.tableView beginUpdates];
}
- (void)controllerDidChangeContent:(NSFetchedResultsController*)controller { ... }
- (void)controller:(NSFetchedResultsController*)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath { ... }
- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id<NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type { ... }
我創建了我希望保存的實體:
Project* newProject = [NSEntityDescription insertNewObjectForEntityForName:@"Project" inManagedObjectContext:self.managedObjectContext];
ProjectDetailViewController* detail = [[ProjectDetailViewController alloc] initWithStyle:UITableViewStyleGrouped
delegate:self
selector:@selector(finishedAdding:)
project:newProject];
後來,我保存:
- (void)save {
// NSLog(@"ProjectDetailViewController.save");
self.project.name = projectNameTextField_.text;
NSError* error;
BOOL b = [self.project.managedObjectContext save:&error];
if (!b) {
NSLog(@"Error saving project!");
} else {
NSLog(@"Project was successfully saved.");
[delegate_ performSelector:selector_ withObject:self.project];
}
[self dismissModalViewControllerAnimated:YES];
}
這一切都工作得很好,除了事實,我的委託方法不火。很顯然,我的表格視圖沒有得到更新,查看新數據的唯一方法是顯式刷新或重新啓動應用程序。
我已經看過了CoreData食譜應用程序 - 但似乎無法找到我失蹤的東西。思考?
-Luther
路德,你可以確認實體和謂詞你配置的NSFetchedResultsController? – ohhorob 2009-12-22 09:38:27