NSNotification 和多线程
我试图在我的多线程应用程序中获取通知 NSTaskDidTerminateNotification
但无法使其正常工作。当我在单线程应用程序上测试它时,它似乎确实有效。在 init
中,我有 [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(taskDidEnd:) name: NSTaskDidTerminateNotification object: myTask];
我很确定它被调用是因为其他对象(如 myTask)正在那里启动。 taskDidEnd:
方法定义为
- (void)taskDidEnd: (NSNotification *)aNotification
{
NSLog(@"Task succeeded.");
}
在 dealloc 中观察者被删除。
这一切都发生在一个在单独线程内启动的对象中,我希望在同一对象内接收该通知。
I'm trying to get the notification NSTaskDidTerminateNotification
in my multithreaded app but I can't get it working. It does seem to work when I tested it on a single threaded app. In init
I have [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(taskDidEnd:) name: NSTaskDidTerminateNotification object: myTask];
and I'm quite sure that it gets called because other objects (like myTask) are being initiated there. And the taskDidEnd:
method is defined as
- (void)taskDidEnd: (NSNotification *)aNotification
{
NSLog(@"Task succeeded.");
}
And in dealloc the observer gets removed.
This all happens in an object which is initiated inside a separate thread and I would like to receive that notification inside the same object.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否在该线程上运行了运行循环?如果没有,NSTask 将不会注意到任务已结束(或者任务尚未结束)并且不会发布通知。
Did you run the run loop on that thread? If not, NSTask won't notice that the task ended (or the task won't have ended yet) and won't post the notification.