HarriesChen
HarriesChen

Reputation: 25

Update UIViewController when Core Data objects are updated

I want binding the dataSource and view controller

I use Core Data, and the dataSource is the an collection of NSManagedObject, when receive web socket notification, I update the data in Core Data, How can I let the view controller update UI automatically.

Upvotes: 0

Views: 187

Answers (2)

Sagar D
Sagar D

Reputation: 2618

You can use NSFetchedResultsController if you are using a UITableView. Else, you can use NSNotificationCenter. Demo use :

In your ViewController,

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

Post a notification to after updating the data

 [[NSNotificationCenter defaultCenter] postNotificationName:@"updateUI" object:nil];

Then in the method updateUI, you can do your UI updating stuff.

Upvotes: 1

Avi Tsadok
Avi Tsadok

Reputation: 1843

One option is to use NSFetchResultsController, if your controller looks like a list of items. The other option is to listen to the web socket notification, and just reload the UI whenever you get an answer.

Upvotes: 0

Related Questions