收到通知的次数多于发送通知的次数
我有一个奇怪的问题。
我有一个通过调用通知中心来发送通知的方法,如下所示:
[[NSNotificationCenter defaultCenter] postNotificationName:@"NIDNewDataSetNotification" object:self];
如果我在发送通知之前 NSLog 时间,我会看到它每秒发送一次,这是应该的。
我在另一个对象中有一个方法,将自己添加为观察者,如下所示:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveDataNotification:) name:@"NIDNewDataSetNotification" object:nil];
但是,通过 NSLogging 时间,我看到它每秒收到通知四次(所有四次都在大约 0.001 秒内)。
没有发送其他通知。如果我注释掉发送通知的行,则捕获通知的方法不会被调用。
有什么想法吗?
I'm having a strange problem.
I have a method that sends off notifications by calling into the notification center like this:
[[NSNotificationCenter defaultCenter] postNotificationName:@"NIDNewDataSetNotification" object:self];
If I NSLog the time right before sending the notification, I see that it sends every second, as it should.
I have a method in another object that adds itself as an observer like so:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveDataNotification:) name:@"NIDNewDataSetNotification" object:nil];
However, by NSLogging the time, I see that it receives the notification four times every second (all four within like .001 seconds).
There are not other notifications being sent. If I comment out the line that sends the notification, my method that catches the notification does not get called.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我可以想到可能发生这种情况的两种情况:
1)您是否以某种方式多次注册通知?也许在注册之前抛出一个 NSLog(@"registering...") 语句以确保您只看到它一次。
2) 您要取消注册该活动吗?可能会被破坏或不被调用吗?也许也可以在那里放置一个 NSLog 语句,以确保它被调用,并确保您正在删除正确的委托和观察者。通知。
I can think of two cases where this might happen:
1) Are you somehow registering for the notification multiple times? Maybe throw an
NSLog(@"registering...")
statement right before you register to make sure you only see it once.2) Are you unregistering for the event? Could that be broken or not called? Maybe put an NSLog statement there too to make sure it's getting called, and make sure you're removing the observer for the correct delegate & notification.
是的,我怀疑您每次设置观察者时都会重新注册,而在删除观察者时不会取消注册。
Yeah, I suspect that you're re-registering each time you set up your observer, and not de-registering when you take it down.