为什么我的 NSNotification 其观察者被多次调用? [英] Why is my NSNotification its observer called multiple times?
问题描述
在一个应用程序中,我使用了几个视图控制器.在一个视图控制器上,观察者被初始化如下:
Within an App I make use of several viewcontrollers. On one viewcontroller an observer is initialized as follows:
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"MyNotification" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myMethod:) name:@"MyNotification" object:nil];
即使在初始化 myMethod:
的执行次数之前删除 NSNotification
时,也会通过相应视图控制器上的重复视图数量来总结.
Even when removing the NSNotification
before initializing the number of executions of myMethod:
is being summed up by the amount of repeated views on the respective viewcontroller.
为什么会发生这种情况以及如何避免 myMethod: 被多次调用.
Why does this happen and how can I avoid myMethod: being called more then once.
注意:我通过使用断点确保在多次调用 postNotification 时没有出错.
Note: I made sure by using breakpoints that I did not made mistakes on calling postNotification multiple times.
这就是我的 postNotification 的样子
This is how my postNotification looks like
NSArray * objects = [NSArray arrayWithObjects:[NSNumber numberWithInt:number],someText, nil];
NSArray * keys = [NSArray arrayWithObjects:@"Number",@"Text", nil];
NSDictionary * userInfo = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
[[NSNotificationCenter defaultCenter] postNotificationName:@"myNotification" object:self userInfo:userInfo];
即使将我的订阅移至 viewwillappear:我得到相同的结果.myMethod:被多次调用.(我重新加载视图控制器的次数).
edit: even after moving my subscribing to viewwillappear: I get the same result. myMethod: is called multiple times. (number of times i reload the viewcontroller).
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"MyNotification" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myMethod:) name:@"MyNotification" object:nil];
}
我的生命周期似乎有问题.ViewDidUnload 和 dealloc 没有被调用,但是 viewdiddisappear 被调用.
edit: something seems wrong with my lifecycle. ViewDidUnload and dealloc are not getting called, however viewdiddisappear is getting called.
我将 Viewcontroller 推送到堆栈的方式如下,其中 parent 是 tableview 的子类(单击此视图控制器启动的行:
The way I push my Viewcontroller to the stack is as follows where parent is a tableview subclass (on clicking the row this viewcontroller is initiated:
detailScreen * screen = [[detailScreen alloc] initWithContentID:ID andFullContentArray:fullContentIndex andParent:parent];
[self.navigationController pushViewController:screen animated:YES];
解决方案:
将 nsnotification 的移除移至 viewdiddisappear 就成功了.谢谢指导!
Moving removal of nsnotification to viewdiddisappear did the trick. Thanks for guidance!
推荐答案
基于此描述,一个可能的原因是您的视图控制器被过度保留并且没有在您认为的时候被释放.如果过度保留,即使使用 ARC,这也很常见.因此,您认为给定的视图控制器只有一个实例处于活动状态,而实际上您有多个活动实例,并且它们都在监听通知.
Based on this description, a likely cause is that your viewcontrollers are over-retained and not released when you think they are. This is quite common even with ARC if things are over-retained. So, you think that you have only one instance of a given viewcontroller active, whereas you actually have several live instances, and they all listen to the notifications.
如果我遇到这种情况,我会在 viewcontroller 的 dealloc 方法中放置一个断点,并确保它被正确释放,如果这是您应用的预期设计.
If I was in this situation, I would put a breakpoint in the viewcontroller’s dealloc method and make sure it is deallocated correctly, if that’s the intended design of your app.
这篇关于为什么我的 NSNotification 其观察者被多次调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!