iPhone - 测试是否存在通知 [英] iPhone - testing if a notification exists
问题描述
在某个代码中,我可以添加类似
At some point in a code one may add something like
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doSomething) name:@"Hello" object:nil];
如何测试此通知是否已在队列中处于活动状态或已被删除,以防止添加重复?
How do I test if this notification is already active on the queue or has been removed, to prevent adding a duplicate?
谢谢。
推荐答案
如果你的意思是测试对于你是否已经注册为通知的观察员,我认为除了发布通知并看到你收到回调(可能带来灾难性后果)之外,还有一种简单的方法。
If you mean "testing for whether you've already registered as an observer for the notification", I don't think there's an easy way apart from posting the notification and seeing you get a callback (with possibly disastrous effects).
如果存在双重添加的危险,我通常会使用 [[NSNotificationCenter defaultCenter] removeObserver:self name:foo object:bar]
添加。
If there's a danger of a double-add, I usually use [[NSNotificationCenter defaultCenter] removeObserver:self name:foo object:bar]
before the add.
在队列中不会注册通知。
Registering for notifications does not happen on a queue.
这篇关于iPhone - 测试是否存在通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!