Search code examples
core-datansfetchedresultscontrollernsmanagedobjectcontext

Core data child tableview not refreshing


I have a one (troop) to many (members) relationship using core data. I can successfully add and delete to the troops.

Once troops are created, when I click on a troop from a tableview, it loads a new table of members of that troop. I can successfully add to the members table with correct troop as the parent. Using NSLog, I can see that the member is added. However, the members table does not reload and the new member does not save until I go back and add a new troop. It seems like the only time the context actually saves is when a new troop is added. When a new troop is added, all of the added members will then appear under their correct troops. Completely closing and relaunching the app does not force the member to be saved.

I have tried adding both of these to the insert method:

[self.managedObjectContext save:nil];
[self.managedObjectContext refreshObject:newManagedObject mergeChanges:YES];

I have added this to the controllerDidChangeContent

 [self.managedObjectContext save:nil];
 [self.tableView reloadData];
 [[NSNotificationCenter defaultCenter] postNotificationName:@"ContextDidSave" object:self.managedObjectContext];

I have added this to the contextDidSave method:

[self.managedObjectContext mergeChangesFromContextDidSaveNotification:notification];

I have added this to the viewWillAppear method:

   [self.tableView reloadData];

And this to the viewDidLoad method:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(contextDidSave:)
                                             name:@"ContextDidSave" object:nil];

I am sure there is something missing or reason why it is not saving. I have also used all of the delegates.

<NSFetchedResultsControllerDelegate, UINavigationControllerDelegate, UITableViewDelegate, UITableViewDataSource>

I have tried every example or suggestion I can find in stackoverflow. I hope someone has new suggestions. Thanks!


Solution

  • From my comment

    Did you set the delegate for you NSFetchedResultsController?

    fetchedController.delegate = self;