I can’t understand why in my life the NSFetchedResultsControllerDelegate methods do not start when I add data to the underlying data store. Data immediately appears if I restart the iPhone application.
I subclassed a UITableViewController and matched NSFetchedResultsControllerDelegate:
@interface ProjectListViewController : UITableViewController <NSFetchedResultsControllerDelegate> {
NSFetchedResultsController* fetchedResultsController_;
NSManagedObjectContext* managedObjectContext_;
}
I create an instance of NSFetchedResultsController and set a delegate for myself:
fetchedResultsController_ = [[NSFetchedResultsController alloc] initWithFetchRequest:request
managedObjectContext:self.managedObjectContext
sectionNameKeyPath:@"Client"
cacheName:@"ProjectsCache"];
fetchedResultsController_.delegate = self;
I implement delegate methods:
- (void)controllerWillChangeContent:(NSFetchedResultsController*)controller {
NSLog(@"ProjectListViewController.controllerWillChangeContent");
[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 { ... }
I create an object that I want to save:
Project* newProject = [NSEntityDescription insertNewObjectForEntityForName:@"Project" inManagedObjectContext:self.managedObjectContext];
ProjectDetailViewController* detail = [[ProjectDetailViewController alloc] initWithStyle:UITableViewStyleGrouped
delegate:self
selector:@selector(finishedAdding:)
project:newProject];
And then I saved it:
- (void)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];
}
Everything works fine except that my delegate methods do not work. Obviously, my table view is not being updated, and the only way to see the new data is to explicitly update or restart the application.
CoreData Recipe, , . ?
-Luther