Note that there are some explanatory texts on larger screens.

plurals
  1. POPreventing re-entering observeValueForKeypath recursively when KVO fired from model setter
    text
    copied!<p>I have a whole host of models that watch themselves for changes. When a setter is triggered, the observer in the model is called and within that model I make a call to a web server and update information in the web server so it correctly has the data it should.</p> <p>However, in my call to the web server I can get back an error for anything from connection being dropped, server reset etc to the server saying the update wasn't allowed (e.g. some other parameter changed by another user prevented the update).</p> <p>Oh the joys of a disconnected data store...</p> <p>Is there any way that I can update <code>self</code> (i.e. the model) without it re-triggering a KVO action?</p> <p>I could turn off the observer when I get back the response, but another user action to the same object is possible while the background server request is being performed. This would then break the other call.</p> <p>See the post <a href="https://stackoverflow.com/questions/3374132/using-one-setter-for-all-model-ivars">here</a> for an explanation of what I was trying to do to get the observation happening in the first place and a code example of the full model.</p> <p>The portion where I am making the call would be in the observer as follows:</p> <pre><code>-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { if ([keyPath isEqual:@"firstName"]) { if !([serverCall value:[change objectForKey:NSKeyValueChangeNewKey] forKey:@"firstName"]) { // Notify the user of the problem and somehow do a // [self setFirstName:[change objectForKey:NSKeyValueChangeOldKey]] // without firing off a KVO which would only loop this process } } if ([keyPath isEqual:@"lastName"]) { // Do whatever I need to do } } </code></pre> <p>You guys always seem to come through on some good advice and I am always grateful for any suggestions you may have.</p>
 

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