kangaroo
kangaroo

Reputation: 105

fetchedResultsController not updating row one properly

I'm testing a simple Core Data project with Entity "Customer" and attribute "name". I'm using a modified version of the Master Detail template; the Master contains a tableview of the names, and the detail is used to enter the name.

In Master's prepareForSegue, I set Details' Customer object as follows:

...
else if ([segue.identifier isEqualToString:@"Add Customer"]) {
    [[segue destinationViewController] setDelegate:self];
    [[segue destinationViewController] setCust:(Customer *) [NSEntityDescription insertNewObjectForEntityForName:@"Customer"inManagedObjectContext:self.managedObjectContext]];
}

Master's insertNewObject adds a generic name:

...
[newManagedObject setValue:sender forKey:@"name"];
...

In Detail, the name is added through a delegate:

- (IBAction)save:(UIBarButtonItem *)sender {
self.cust.name = self.customerName.text;
[self.delegate addNameController:self selectedSave:YES];  

Master's delegate method:

- (void)addNameController:(AddNameController *)controller selectedSave:(BOOL)save {
if (!save) {
    [self.managedObjectContext deleteObject:controller.cust];
}
NSError *error = nil;
if (![self.managedObjectContext save:&error]) {
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
}
[self dismissViewControllerAnimated:YES completion:nil];}

This all works fine and the tableview updates properly for all new name entries except any entry that, sort wise, becomes the first entry. In that case, although the name is added to the db, a blank entry is placed in the tableview's first row. I'm able to reveal the name by either tapping the Edit button or by adding a new name that sorts ahead of the invisible name.

To get around this problem, I've forced a reload in Master:

- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.tableView reloadData];}

This seems like a bit of a hack to me. It was my understanding that Core Data would take care of properly updating the display in response to any content changes.

Is there an issue with the way I'm implementing this or is this an idiosyncrasy with Core Data?

Thanks.

Upvotes: 3

Views: 201

Answers (1)

Mundi
Mundi

Reputation: 80271

Actually, the table will have to be explicitly updated, unless you have a method linked to the NSFetchedResultsController delegate where you do this. (The delegate method would be controllerDidChangeContent:.)

A more convenient and obvious way would be to do it right in your master's delegate callback when dismissing the detail view. This is not a "hack".

Upvotes: 1

Related Questions