• 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: Right place to unobserve notifications in a document
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Right place to unobserve notifications in a document


  • Subject: Re: Right place to unobserve notifications in a document
  • From: Quincey Morris <email@hidden>
  • Date: Wed, 20 Aug 2008 10:07:34 -0700

On Aug 20, 2008, at 07:58, Joan Lluch (casa) wrote:

I understand what is your preferred approach, which it is also what I would do. I can add to it that I even thought that you *have to* *always* unregister the observers when you have done with them. This is obviously true in RC environment, but this is apparently not true in the case of GC. This is the part I don't get. I would appreciate if someone would clarify it for me.

This thread has wandered off into confusion because it mixes up 2 unrelated kinds of "observer". These two mechanisms have absolutely nothing in common, except the word "observe".




Under GC, notification centers keep weak references to their notification observers. As Michael pointed out, these references are automatically zeroed when the observer goes away, so the notification center can detect the situation and clean itself up accordingly. This does *not* mean that an observer can't be removed until it goes through its finalize -- removeObserver: can of course be called at any time.

In a non-GC app, you always have to call removeObserver:, and the observer's dealloc is a reasonable place to ensure that it's been done before the observer goes away. Under GC, there's no equivalent of dealloc, but happily there's no need to ensure that it's been done.



Under GC, KVO keeps a strong reference to the KVO observer in (or, rather, in a private constructed-on-the-fly sort-of-subclass of) the observed object. If the observer goes away, the strong reference is left dangling and will usually result in an exception. With luck, Apple will decide to change the KVO implementation sometime to keep weak references, so that such dangling references won't exist. If it makes the change, that also does not mean that an observer can't be removed except by being finalized -- removeObserver:forKeyPath: can of course be called at any time.

In a non-GC app, again, dealloc is a reasonable place to ensure that removeObserver:forKeyPath: has been called. Under GC (until Apple decides to change KVO), there's no way to *ensure* that the observation has been "cancelled" before the observer goes away. You have to rely on someone calling it earlier, and it's sometimes a significant programming headache to make sure this gets done properly.

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >Right place to unobserve notifications in a document (From: Negm-Awad Amin <email@hidden>)
 >Re: Right place to unobserve notifications in a document (From: "Michael Ash" <email@hidden>)
 >Re: Right place to unobserve notifications in a document (From: Negm-Awad Amin <email@hidden>)
 >Re: Right place to unobserve notifications in a document (From: Andy Lee <email@hidden>)
 >Re: Right place to unobserve notifications in a document (From: Negm-Awad Amin <email@hidden>)
 >Re: Right place to unobserve notifications in a document (From: Andy Lee <email@hidden>)
 >Re: Right place to unobserve notifications in a document (From: Negm-Awad Amin <email@hidden>)
 >Re: Right place to unobserve notifications in a document (From: "Joan Lluch (casa)" <email@hidden>)
 >Re: Right place to unobserve notifications in a document (From: Negm-Awad Amin <email@hidden>)
 >Re: Right place to unobserve notifications in a document (From: "Joan Lluch (casa)" <email@hidden>)

  • Prev by Date: Re: Simple NSView Question
  • Next by Date: Re: mouseMoved events and unfocused views
  • Previous by thread: Re: Right place to unobserve notifications in a document
  • Next by thread: Re: Right place to unobserve notifications in a document
  • Index(es):
    • Date
    • Thread