Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the NSNotification retain the object?

Tags:

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]; 
like image 768
MDMonty Avatar asked Mar 17 '11 21:03

MDMonty


1 Answers

"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 use NSNotificationQueue. 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]; 
like image 53
NSGod Avatar answered Sep 23 '22 02:09

NSGod