Re: notifications vs delegates
Re: notifications vs delegates
- Subject: Re: notifications vs delegates
- From: email@hidden
- Date: Wed, 27 Feb 2002 13:34:34 -0800
I had some speed problems with notifications in one program I
was writing, but I was able to speed it up by using
NSNotificationQueue, which lets you merge multiple notifications
with the same name and/or object into a single notification.
Yes, this can be a very useful tool.
For the record, delegate objects are actually not typically
retained. This is because in the most common case, your
delegate object that already has a reference to the view object,
so if the view object were to retain the delegate, it would
result in a retain cycle and thus a memory leak.
You are correct, I spoke in haste.
In fact, this can be an issue that bites beginning Cocoa programmers.
If you set yourself as the delegate of some other object, you should be
sure to do [someObject setDelegate:nil] in your dealloc (or before). If
you don't do this, someObject will have an unretained reference to it's
delegate (you), and the next time it tries to send a message to its
delegate, boom. This bit me several times before I developed the habit
of always adding my -setDelegate:nil call to my code immediately after
adding my setDelegate:self call -- you need to tie these changes
together in your mind.
This is also an issue for adding yourself as the observer for
notifications, as your object is not retained by the notification
mechanism. You need to remove yourself as an observer in your dealloc
(or before).
The messages that are sent out as notifications are actually
sent to the delegate as notifications as well. When the
delegate is set, NSApplication will add the delegate as an
observer for all the notifications, using the selectors
specified in the NSApplication docs. But any other object can
then come along and listen for the same notifications as well.
Yes, the Kit does it this way in NSApplication (and some other places,
I think). It struck me at first as being a bit messy (the delegate
shouldn't have to muck about with notification objects, it should get
all its information in individual parameters), but then I realized it's
actually a nice design, as it lets you very easily shift an object from
being a notification listener to being an actual delegate, or vice
versa, without having to rewrite all the relevant methods.
Ben Haller
Stick Software
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.