ios 如何阻止 NSNotification 中的观察者调用两次?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7751191/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to stop the Observer in NSNotification to called twice?
提问by Azhar
I have an observer of NSNotification
which is called twice. I do not know what to do with it.
我有一个观察者NSNotification
被调用了两次。我不知道该怎么办。
I googled it but no solution found.
我用谷歌搜索但没有找到解决方案。
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(connectedToServer:) name:@"ConnectedToServer" object:nil];
- (void)connectedToServer:(NSNotification*)notification {
[[NSNotificationCenter defaultCenter] postNotificationName:@"SendMessageToServer" object:message];
}
回答by EmptyStack
Solution 1:The first thing is to check if the notification itself is posted twice.
解决方案 1:首先要检查通知本身是否发布了两次。
Solution 2:Even if the notification is posted only once, the actionwill be called as many times you've added the observer for the notification (no matter the notification is same or not). For example, the following two lines will register the observer(self
) for the same notification(aSelector
) twice.
解决方案 2:即使通知仅发布一次,该操作也会被调用与您为通知添加观察者的次数相同(无论通知是否相同)。例如,以下两行将self
针对同一个通知(aSelector
)注册观察者()两次。
[[NSNotificationCenter defaultCenter] addObserver:self selector:aSelector name:aName object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:aSelector name:aName object:nil];
You have to find where you are adding observer for the second time, and remove it. And also make sure that the code where you are add the observer is not called twice.
您必须找到第二次添加观察者的位置,然后将其删除。还要确保添加观察者的代码没有被调用两次。
Solution 3:If you are not sure whether you have already added the observer or not, you can simply do the following. This will make sure that the observer is added only once.
解决方案 3:如果您不确定是否已经添加了观察者,您可以简单地执行以下操作。这将确保观察者只添加一次。
[[NSNotificationCenter defaultCenter] removeObserver:self name:aName object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:aSelector name:aName object:nil];
回答by tfrank377
If your addObserver
method is run multiple times, it will create multiple observers. My issue was that I somehow placed my observer in viewWillAppear
which appeared multiple times before I posted the notification and it resulted in my observer being called multiple times.
如果您的addObserver
方法多次运行,它将创建多个观察者。我的问题是,viewWillAppear
在发布通知之前,我以某种方式将我的观察者放置在其中多次出现,这导致我的观察者被多次调用。
While EmptyStack's 3rd solution works, there is a reason your observer is being called twice, so by finding it, you can prevent needless lines of code instead of removing and adding the same observer.
虽然 EmptyStack 的第三个解决方案有效,但您的观察者被调用两次是有原因的,因此通过找到它,您可以防止不必要的代码行,而不是删除和添加相同的观察者。
I would suggest putting your observer in viewDidLoad
to avoid simple errors like the one I experienced.
我建议让你的观察者参与viewDidLoad
以避免像我所经历的那样的简单错误。
回答by bhadresh
Try to removeObserver in viewWillDisappear method :
尝试在 viewWillDisappear 方法中 removeObserver :
-(void)viewWillDisappear:(BOOL)animated{
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"startAnimating" object:nil]; }
回答by Joyson
For those looking for a solution in Swift 2.2 and above and who have reached this question like me you can create an extension as follows :
对于那些在 Swift 2.2 及更高版本中寻找解决方案并像我一样遇到这个问题的人,您可以创建一个扩展如下:
import Foundation
extension NSNotificationCenter {
func setObserver(observer: AnyObject, selector: Selector, name: String?, object: AnyObject?) {
NSNotificationCenter.defaultCenter().removeObserver(observer, name: name, object: object)
NSNotificationCenter.defaultCenter().addObserver(observer, selector: selector, name: name, object: object)
}
}
You can call this method as follows :
您可以按如下方式调用此方法:
NSNotificationCenter.defaultCenter().setObserver(self, selector: #selector(methodName), name: "name", object: nil)
The extension will handle the removal of previous observer if it exists. Even if there was no previous observer present this code won't crash.
如果存在,扩展将处理移除先前观察者。即使没有先前的观察者在场,此代码也不会崩溃。
回答by stcui
Try set a breakpoint on [[NSNotificationCenter defaultCenter] addObserver:self selector:aSelector name:aName object:nil];
and check if it is called more than once.
尝试设置断点[[NSNotificationCenter defaultCenter] addObserver:self selector:aSelector name:aName object:nil];
并检查它是否被多次调用。
回答by Vladimír Slavík
I had two instances of the same class and it took me some time before I have realised that the notification is not running twice in one instance of that class but twice in two instances.
我有同一个类的两个实例,我花了一些时间才意识到通知不是在该类的一个实例中运行两次,而是在两个实例中运行两次。