AOY
AOY

Reputation: 355

How can I make tableViewController know about managedObjectContext changed outside?

In the app I'm developing I've got 2 tableViewControllers with tables filled with the data using Core Data and 2 ViewControllers in which I can read, add, delete and update that information. Everything is great with this except the fact that tableViewControllers don't get updated. I was thinking about using delegation to somehow make those tableViews know that didChangeObject happened in another ViewController but I really don't know how to do it and I'm not sure that this is the right way to do it.

Upvotes: 1

Views: 50

Answers (2)

Leo
Leo

Reputation: 24714

I suggest you to use NSFetchedResultsController as Marcus post.It is good to work with tableview.

If you do not want to use it,you can use notification

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(managedObjectDidChange:) name:NSManagedObjectContextDidSaveNotification object:nil];

Then merge change

-(void)managedObjectDidChange:(NSNotification *)nofication{
[self.context performBlock:^{
    [self.context mergeChangesFromContextDidSaveNotification:nofication];
}];
}

Upvotes: 3

Marcus S. Zarra
Marcus S. Zarra

Reputation: 46718

This is exactly what the NSFetchedResultsController is designed for. You use the NSFetchedResultsController to drive the data for your table views and make your table view controller a delegate to the NSFetchedResultsController to receive updates about when the data changes.

Upvotes: 4

Related Questions