Re: Re: KVO question
Re: Re: KVO question
- Subject: Re: Re: KVO question
- From: "Peter Boctor" <email@hidden>
- Date: Tue, 23 May 2006 07:03:27 -0700
On 5/22/06, Sean Murphy <email@hidden> wrote:
On May 22, 2006, at 8:29 PM, Peter Boctor wrote:
> I have a class "foo" that keeps track of a "current" object.
> Another class
> wants to know:
>
> 1. When foo's "current" object changed
> 2. When the property "bar" on foo's "current" object has changed.
>
> and uses the following code:
>
> [foo addObserver:self
> forKeyPath:@"current"
> options:NSKeyValueObservingOptionNew
> context:NULL];
>
> [foo addObserver:self
> forKeyPath:@"current.bar"
> options:NSKeyValueObservingOptionNew
> context:NULL];
>
> When the "current" object changes, observeValueForKeyPath is called
> twice,
> once with a keyPath of "current" and a second time with a keyPath
> of "
> current.bar". How can the observeValueForKeyPath implementation
> determine
> what actually changed, "current" or "current.bar"?
Hey Peter,
The only suggestion that I could think of is to disable automatic key-
value observing on the bar property of the foo object. Then, in your
accessor method for the bar object, you can determine if the bar is
actually being changed without a complete replacement of the entire
foo object beneath it (for instance: is an init method setting this
value for a brand new object?). After you verify that bar is
changing and you want KVO to activate, call willChangeValueForKey and
didChangeValueForKey to inform the observer that it should deal with
an actual change of the bar property.
Disabling automatic notification isn't an all-or-nothing option,
meaning you can selectively turn it off for only the bar key.
Check out the ADC docs for more information on Manual KVO: http://
developer.apple.com/documentation/Cocoa/Conceptual/KeyValueObserving/
Concepts/AutoVsManual.html#//apple_ref/doc/uid/20001844-178876
This probably isn't the only solution, but at least something you can
work with..
Hope it helps,
-Murph
Sean,
Unfortunately it's the notification for "current" that causes
observeValueForKeyPath to be called twice, first with a path of "current"
and then with "current.bar".
I can't quite say that this behavior is incorrect since if the current
object changed then current.bar also changed.
But I need a way to distinguish between a notification that happened because
current changed versus a notification that happened because the foo property
of current changed.
-peter
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden