我无法弄清楚为什么在我将数据添加到底层数据存储时,NSFetchedResultsControllerDelegate 方法没有触发。如果我重新启动 iPhone 应用程序,数据会立即显示。
我继承了 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 Recipe 应用程序 - 但似乎找不到我缺少的东西。想法?
——路德