views:

25

answers:

1

Hi,

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 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 :)

+1  A: 

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];
    }
}
Max Seelemann
This was exactly what I needed to accomplish! Thank you so much for your quick and accurate response!