I'm looking for a way to "monitor/observe" a singleton class I'm using to pass information back from an asynchronous http request. I want to update the UI once I have received and parsed the response. Since iPhone SDK does not have an NSArrayController to monitor my data in the singleton, how can I do an asynchronous开发者_StackOverflow UI update?
This is how I have my logic separated:
Singleton: (hold's array with data)
RESTClient: retrieves xml from a remote service and saves to array in singleton
ViewController: need to monitor for changes to singleton's data and update UI via a function
Thank you in advance :)
While iOS does not have NSArrayController
, it does have all the techniques this is build on top. Specifically, this is Key-Value-Binding and more fundamental Key-Value-Observation. The latter should be your tool of choice. It's pretty easy to do:
1) Register your controller as an observer, like that:
[singleton addObserver:self forKeyPath:@"myData" options:0 context:@"data"];
2) Observe all changes as they come in:
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if (context == @"data") {
// Do whatever you need to do...
}
else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
精彩评论