Re: Where's the best place for addObserver and removeObserver
Re: Where's the best place for addObserver and removeObserver
- Subject: Re: Where's the best place for addObserver and removeObserver
- From: Keary Suska <email@hidden>
- Date: Thu, 17 Jul 2008 09:34:42 -0600
- Thread-topic: Where's the best place for addObserver and removeObserver
7/17/08 8:59 AM, also sprach email@hidden:
> However, the way I am doing it makes me wonder where the best place
> should be for adding and removing the observers. What I am currently
> doing is adding the observers in the awakeFromNib methods of each
> viewController, and removing them in the dealloc method of the same
> controller. I think that adding the observers in awakeFromNib methods
> is fine, but the fact that I retain the view controllers implies that
> such controllers are still observing the application delegate even if
> they are not currently showing. Is this a good coding/design pattern?.
>
> Furthermore, if I eventually switch to Garbage Collection in the
> future, those observers will not ever be removed. Or worse, in case I
> decided not to keep a reference in my window controller, and the GC
> eventually claims an unused view controller, then what will happen to
> the observers.
>
> Would someone bring a light to this.? Basically what i am asking is,
> where is the recommended place to add and to remove observers set up
> in views (or view controllers)?
I don't think it matters when you *start* observing, as long the the
observed objects are guaranteed to exist. awakeFromNib is a perfect place if
you expect the observation to last the lifetime of the nib objects.
I will say that removing observation in dealloc is a dangerous endeavor. It
works if you can guarantee that the observer will be deallocated *before*
the observed. This guarantee is difficult if not impossible in a nib-loaded
situation, AFAIK.
I believe that the bindings mechanism, however, has ways to deal with these
issues in a nib-loaded situation (and generally as well). That might be an
avenue to pursue.
HTH,
Keary Suska
Esoteritech, Inc.
"Demystifying technology for your home or business"
_______________________________________________
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