• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: KVO question
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: KVO question


  • Subject: Re: KVO question
  • From: Sean Murphy <email@hidden>
  • Date: Tue, 23 May 2006 00:12:04 -0400


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
_______________________________________________
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


  • Follow-Ups:
    • Re: Re: KVO question
      • From: "Peter Boctor" <email@hidden>
References: 
 >KVO question (From: "Peter Boctor" <email@hidden>)

  • Prev by Date: Re: NSWindowController confusion redux
  • Next by Date: Re: Virtual folder
  • Previous by thread: KVO question
  • Next by thread: Re: Re: KVO question
  • Index(es):
    • Date
    • Thread