NSNotification 是否保留该对象?

发布于 2024-10-23 18:55:19 字数 628 浏览 3 评论 0原文

我的问题是关于添加到 -postNotificationName:object: userInfo: 方法的对象。

NSNotification 是否保留该对象? (与 NSMutableDictionary 或 Array 类似)...意味着我可以在发布通知后释放该对象

下面是一个代码片段,可帮助描述我的问题...释放该对象是否有效。 Apple 文档的链接可能非常有帮助。

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];

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];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

椵侞 2024-10-30 18:55:19

“NSNotification 是否保留
目的 ? (以类似的方式
NSMutableDictionary 或数组)...
意味着我可以在之后释放该对象
发布通知”

我不确定该方法是否保留 objectuserInfo 参数,但实际上,这并不重要。

我认为您可能会设想 NSNotificationCenter 正在创建这些通知并以异步方式广播它们,但事实并非如此,正如 NSNotificationCenter 的文档中所述(请参阅<. a href="http://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSNotificationCenter_Class/Reference/Reference.html#//apple_ref/doc/uid/20000219-1265" rel= "noreferrer">NSNotificationCenter 类参考),通知同步发布:

通知中心提供
给观察员的通知
同步地。换句话说,
postNotification: 方法不
返回,直到所有观察者都完成
收到并处理了
通知。发送通知
异步使用
NSNotificationQueue。在一个
多线程应用程序,
通知始终发送至
通知所在的线程
已发布,可能不一样
观察者注册的线程
本身。

因此,在您的代码中,通知中心创建通知,然后通过默认中心广播它。已注册此通知名称和对象组合的任何对象都将接收该通知,然后执行它们在注册该通知时指定的选择器。然后,控件返回到发布通知的类。

换句话说,当您的代码到达[teamDictCopy release]行时,teamDictCopy将已经被所有感兴趣的各方“使用”。所以,释放它应该不会有任何危险。

只是关于约定的注释。一般来说,object: 参数是发布通知的对象,userInfo: 参数是NSDictionary额外信息。因此,通常情况下,您会按如下方式处理通知:

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];

"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];
苦行僧 2024-10-30 18:55:19

是的 - 一旦该对象被设置为通知的对象,您就可以释放该对象。

你也可以子类化。

至于具体的文件/声明:我具体不记得了。

然而,当类型被识别为对象时,这是对象、它们的实例变量以及分布式通信和信令的基础。

我已经为你写了一个测试,所以你可以放心。如果不保留对象,通知的用例将会很少。只需在指示处添加断点,然后在启用断点的情况下运行即可。享受!

#import <Foundation/Foundation.h>

@interface MONObject : NSObject
@end

@implementation MONObject

- (id)retain {
    return self; /* << add breakpoint here */
}

/* needed to counter retain override
   (although all MONObjects will leak in this example)
*/
- (void)release {
}

@end

int main(int argc, const char* argv[]) {
    NSAutoreleasePool * pool = [NSAutoreleasePool new];

    NSString * name = @"UnComplete";
    MONObject * obj = [MONObject new];
    [[NSNotificationCenter defaultCenter] postNotificationName:name object:obj userInfo:nil];
    [obj release], obj = 0;

    [pool drain];
    return 0;
}

yes - you can release the object once it's been set as the notification's object.

you can also subclass.

as far as a specific document/statement: i don't remember one, specifically.

this is however the basis of objects, their instance variables, and distributed communications and signaling when types are identified as being an object.

i've written a test for you, so you can be assured of this. the use cases of notifications would be few if the object were not retained. just add a breakpoint where instructed, then run with breakpoints enabled. enjoy!

#import <Foundation/Foundation.h>

@interface MONObject : NSObject
@end

@implementation MONObject

- (id)retain {
    return self; /* << add breakpoint here */
}

/* needed to counter retain override
   (although all MONObjects will leak in this example)
*/
- (void)release {
}

@end

int main(int argc, const char* argv[]) {
    NSAutoreleasePool * pool = [NSAutoreleasePool new];

    NSString * name = @"UnComplete";
    MONObject * obj = [MONObject new];
    [[NSNotificationCenter defaultCenter] postNotificationName:name object:obj userInfo:nil];
    [obj release], obj = 0;

    [pool drain];
    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文