为什么我的NSNotification会多次调用它的观察者? [英] Why is my NSNotification its observer called multiple times?

查看:73
本文介绍了为什么我的NSNotification会多次调用它的观察者?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在App中我使用了几个viewcontrollers。在一个viewcontroller上,一个观察者初始化如下:

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

即使在初始化之前删除 NSNotification 也是如此 myMethod:的执行次数通过相应viewcontroller上的重复视图量来总结。

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:被多次调用。 (我重新加载viewcontroller的次数)。

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!

推荐答案

根据这个描述,一个可能的原因是你的viewcontrollers被过度保留而且你认为它们没有被释放。如果事情被过度保留,这种情况很常见。所以,你认为你只有一个一个给定的viewcontroller活动的实例,而你实际上有几个l ive实例,他们都听取通知。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆