Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I solved this problem a couple ways in different apps. </p> <p>One solution is to pass an object and selector along to notify such as:</p> <pre><code>- (id)getUsersAndNotifyObject:(id)object selector:(SEL)selector </code></pre> <p>This breaks the nice property behavior however. If you want to keep the methods as properties, have them return immediately, with either cached data or nil. If you need to go out to the network, do so asynchronous and then let the rest of the app know the data changed via KVO or the NSNotificationCenter. (Cocoa Bindings would be an option on the Mac, but they don't exist on iPhone).</p> <p>The two methods are fairly similar. Register for updates with your shared instance, and then ask for the data. KVO is a little lighter weight if you just dealing with raw observable properties, but an NSNotification might be more convenient if you're interested in several different pieces of data. </p> <p>With an NSNotification, the client object could register for one type of notification which includes the changed data in its userInfo dictionary instead of having to register obvservers for every single key path you're interested in.</p> <p>An NSNotification would also allow you to pass back failures or other status information a lot more easily than straight KVO.</p> <p>KVO method:</p> <pre><code>// register observer first so you don't miss an update [[MySingleton sharedInstance] addObserver:self forKeyPath:@"users" options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld) context:&amp;kvo_users_context]; users = [MySingleton sharedInstance].users; // implement appropriate observeValueForKeyPath:ofObject:change:context: method </code></pre> <p>NSNotification Method:</p> <pre><code> [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sharedDataChanged:) name:MySingletonDataUpdatedNotification object:[MySingletonDataUpdatedNotification sharedInstance]]; users = [MySingleton sharedInstance].users; // implement appropriate sharedDataChanged: method </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload