My question is in regards the object that gets added to a -postNotificationName:object: userInfo:
method.
Does the NSNotification retain the object ? (in a similar fashion to NSMutableDictionary or Array) ... meaning I can release the object after posting the notification
Below is a code snippet to help describe my question ... is it valid to release the object. A link to Apple documentation could be really helpful.
NSMutableDictionary *teamDictCopy = [self.teamDict mutableCopy]; [teamDictCopy setObject:[NSNumber numberWithInt:self.scrollViewIndex] forKey:@"imageIndex"]; if([self.statusButton.title isEqualToString:@"Completed"]){ [[NSNotificationCenter defaultCenter] postNotificationName:@"UnComplete" object:teamDictCopy userInfo:nil]; } [teamDictCopy release];
"Does the NSNotification retain the object ? (in a similar fashion to NSMutableDictionary or Array) ... meaning I can release the object after posting the notification"
I'm not sure if the object
and userInfo
parameters are retained by that method or not, but in practice, it shouldn't really matter.
I think you may be envisioning that NSNotificationCenter
is creating these notifications and broadcasting them in an asynchronous manner, but that isn't the case. As stated in the documentation for NSNotificationCenter
(see NSNotificationCenter Class Reference), notifications are posted synchronously:
A notification center delivers notifications to observers synchronously. In other words, the
postNotification:
methods do not return until all observers have received and processed the notification. To send notifications asynchronously useNSNotificationQueue
. In a multithreaded application, notifications are always delivered in the thread in which the notification was posted, which may not be the same thread in which an observer registered itself.
So, in your code, the notification center creates the notification and then broadcasts it through the default center. Any objects which have registered for this combination of notification name and object will receive the notification and then perform the selector they specified when they registered for that notification. Afterwards, the control returns to the class that posted the notification.
In other words, by the time your code gets to the [teamDictCopy release]
line, the teamDictCopy
will already have been "used" by all of the interested parties. So, there shouldn't be any danger in releasing it.
Just a note on conventions. Generally, the object:
parameter is meant to be the object that is posting the notification, and the userInfo:
parameter is meant for an NSDictionary
of extra information. So, normally, you would handle the notification like follows:
NSMutableDictionary *teamDictCopy = [self.teamDict mutableCopy]; [teamDictCopy setObject: [NSNumber numberWithInt:self.scrollViewIndex] forKey:@"imageIndex"]; if([self.statusButton.title isEqualToString:@"Completed"]){ [[NSNotificationCenter defaultCenter] postNotificationName:@"UnComplete" object:self userInfo:teamDictCopy]; } [teamDictCopy release];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With