Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here is the problem:</p> <p>[self willChangeValueForKey:keyPath]; &lt;--- At this point in time the actual observer needs to unsubscribe to street [self didChangeValueForKey:keyPath]; &lt;--- and add itself to the new value. </p> <p>By not providing the new value you are denying the observer the opportunity to unsubscribe.</p> <p>Here is a hacked version that works and demonstrates the problem.</p> <pre><code>/* --- Bridge ----------------------------------------- */ .... ..... @interface Bridge : NSObject { NSMutableDictionary* observedKeys; NSObject* obj; //**** Dictionary for old values just before we send the didChangeValue notification. NSMutableDictionary * oldValues; } ... ..... - (id)init { if( !( self = [super init] ) ) { return nil; } observedKeys = [NSMutableDictionary new]; //************* Initialize the new dictionary oldValues = [NSMutableDictionary new]; return self; } .... ..... - (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { NSLog( @"&gt;&gt;&gt;&gt; Detected Change in keyPath: %@", keyPath ); // **** Cache the old value before telling everyone its going to change. [oldValues setValue:[change valueForKey:NSKeyValueChangeOldKey] forKey:keyPath]; [self willChangeValueForKey:keyPath]; // **** Simulate the change by removing the old value. [oldValues removeObjectForKey:keyPath]; // **** Now when we say we did change the value, we are not lying. [self didChangeValueForKey:keyPath]; } -(id)valueForUndefinedKey:(NSString*)key { // **** Important part, return oldvalue if it exists id oldValue; if(oldValue = [oldValues valueForKey:key]){ return oldValue; } /* Register an observer for the key, if not already done */ if( ![observedKeys objectForKey:key] ) { [observedKeys setObject:[NSNumber numberWithBool:YES] forKey:key]; NSLog(@"adding observer for:%@", key); [obj addObserver:self forKeyPath:key options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil]; } return [obj valueForKey:key]; } .... ...... </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