Re: Notification Issue
Re: Notification Issue
- Subject: Re: Notification Issue
- From: Shaun Wexler <email@hidden>
- Date: Thu, 11 Sep 2003 15:23:23 -0700
On Sep 11, 2003, at 11:23 AM, John Nairn wrote:
My application has two types of documents and one type needs to know if
the other one has just closed. I implemented the process by posting a
notification in the dealloc method of the second document type with
object self.
But then I noticed in Apple documentation on posting notification that:
notificationWithName:object:
+ (id) notificationWithName: (NSString *) aName object: (id) anObject
Returns a notification object that associates the name aName with the
object anObject .aName may not be nil .
This method copies aName and retains anObject .
My concern is that self (passed as anObject) will be retained while
being deallocated. It seems to work fine, but is such a notification
posting in delloc method forbidden?
No, it's not forbidden. Since the -dealloc is already "happening",
bumping the retain count would probably only cause problems if
something subsequently adds the object to an autorelease pool instead
of releasing it directly. Using "weak reference" non-retained object
values is the answer, since a comparison between any two objects SHOULD
be using an -isEqual: method, but I don't know if Notifications use
this or just compare the object pointers themselves. Since you say
that "closing" is what you need to watch for, then track them by their
window pointers instead.
But if what you described is the behavior you want, declare
@protocol(NonRetainedValueProtocol), and implement it by adding an
instance variable of type NSValue *nonRetainedValue, plus the following
method to any classes (ie your documents), like this:
- (NSValue *)nonRetainedValue
{
if (!nonRetainedValue)
nonRetainedValue = [[NSValue valueWithNonretainedObject:self] retain];
return nonRetainedValue;
}
And this:
- (void)dealloc
{
NSNotificationCenter *defaultCenter = [NSNotificationCenter
defaultCenter];
[defaultCenter removeObserver:self];
[defaultCenter
postNotificationName:@"MyDocumentWillDeallocNotification" object:[self
nonRetainedValue]];
// dealloc all of your instance resources here
// ...
[nonRetainedValue release];
[super dealloc];
}
Any objects can now add themselves as a "non-retaining" observer of
your document "theDocument" like this:
- (void)observeDeallocOfDocument:(id
<NonRetainedValueProtocol>)theDocument
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(documentWillDealloc:)
name:@"MyDocumentWillDeallocNotification" object:[theDocument
nonRetainedValue]];
}
- (void)documentWillDealloc:(NSNotification *)notification
{
id object = [[notification object] nonRetainedObjectValue];
NSLog(@"%@ will dealloc", object);
}
Using this method allows you to easily place your custom objects in any
container class without incrementing the actual object's retain count.
I use -nonRetainedValue in my base object class, which isn't at all
expensive to use. I also have a "CheapObject" base class that performs
its own reference counting, as well as my "SingletonObject" class which
I posted here previously.
Oh, and you could alternately add it directly to NSObject using +poseAs:
Hope that helps.
--
Shaun Wexler
MacFOH
http://www.macfoh.com
_______________________________________________
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.