简体   繁体   English

从一个地方删除所有通知观察者

[英]Removing all notification observer from a single place

I want to remove a notification observer and I am using the method: 我想删除通知观察者,我正在使用该方法:

[[NSNotificationCenter defaultCenter] removeObserver: name:@"myNotification" object:nil];

for this. 为了这。 Now there are many observers who are listening to this notification and I want to remove all of them in one shot from a centralised place. 现在有很多观察者正在听这个通知,我想从集中的地方一次性删除所有这些通知。 Can I pass 'nil' in first parameter and it will remove all observers who are listening to myNotification? 我可以在第一个参数中传递'nil',它会删除所有正在侦听myNotification的观察者吗?

You can remove an object from the notification center all together which means no notifications will get triggered. 您可以一起从通知中心删除对象,这意味着不会触发任何通知。 For example, when I have a view controller that has registered for notifications, I include this line in my dealloc. 例如,当我有一个已注册通知的视图控制器时,我将此行包含在我的dealloc中。

[[NSNotificationCenter defaultCenter] removeObserver:self];

This is at the object level...so it will unregister for many notifications. 这是在对象级别...因此它将取消注册许多通知。 It won't unregister for one notification in many objects. 它不会取消注册许多对象中的一个通知。

Hope I understood your question correctly. 希望我能正确理解你的问题。

In case of Swift, you doing it like this: 在Swift的情况下,你这样做:

NSNotificationCenter.defaultCenter().removeObserver(self)

And in Swift 3: 在Swift 3中:

NotificationCenter.default.removeObserver(self)

Unfortunately, there is no way to remove all observers of a specific notification in one place. 不幸的是,没有办法在一个地方删除特定通知的所有观察者。 While there are certainly cases where this would be nice, it would be a dangerous thing to do as generally, the object doing the observing should be responsible for adding and removing itself as an observer of a particular notification. 虽然有一些情况会很好,但通常情况下这样做是危险的,执行观察的对象应该负责添加和删除自己作为特定通知的观察者。 This ensures no unpredictable behavior b/c observers can come and go so they configure and clean up after themselves. 这确保了b / c观察者不会出现不可预测的行为,因此他们可以自行配置和清理。

If an object that generates notifications goes away, it won't matter to the observer as the observer doesn't know about that object anyway. 如果生成通知的对象消失,则观察者无关紧要,因为观察者无论如何都不知道该对象。 It just means that object won't be generating any more notifications. 它只是意味着对象不会再生成通知。

[EDIT: RESPONSE TO YOUR COMMENT RE CLASS B STOPPING CLASS A FROM OBSERVING] [编辑:对您的评论作出回应而B类从观察中停止A类]

I just saw your comment. 我刚看到你的评论。 There are different ways to accomplish this, particularly if class B knows about class A. As you reference classes it sounds like you want to affect all instances of a class vs a particular instance. 有不同的方法可以实现这一点,特别是如果B类知道A类。当您引用类时,听起来您想要影响类的所有实例与特定实例。 If you have some condition you can check when handling the notification, that's how I would approach this. 如果您有某些条件,您可以在处理通知时进行检查,这就是我如何处理此问题。 In the notification handler something like: 在通知处理程序中,例如:

if ([self shouldRespondToNotificationNamed:notification.name]) {
   [self performNotificationAction];
}

If you don't have a condition you can check, then create one either in the class in question as an iVar or in a place where you can access it globally for all class instances. 如果您没有条件,则可以检查,然后在相关类中创建一个作为iVar,或者在可以全局访问所有类实例的位置。 I generally use a singleton to store global app state that doesn't persist. 我通常使用单例来存储不会持久的全局应用程序状态。 If it persists, then use whatever method you're using for other state. 如果它仍然存在,那么使用你用于其他状态的任何方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 快速从所有视图控制器中删除所有通知观察器 - Removing all notification observer from all view controllers in swift 从另一个 viewController Swift 中删除通知观察者 - Removing a notification observer from another viewController Swift 从通知中心删除Observer的最佳位置在哪里 - Where is the best place to remove Observer from Notification Center 从设备中删除单个发送的远程通知 - Removing a Single Sent Remote Notification From Device View Controller未删除Notification Center观察器 - View controller is not removing Notification Center observer 如果在AppDelegate中为通知添加观察者,我是否需要打扰删除它? - If add an observer for a notification in the AppDelegate, do I need to bother removing it? iOS从通知中删除观察者:我可以为所有观察者调用一次吗? 即使没有? - iOS Remove observer from notification: Can I call this once for all observers? And even if there are none? 从另一个视图控制器中删除通知观察者 - Remove notification observer from another view controller 单击时从通知中心删除通知 - Removing a notification from notification center on click 从 AVPlayer 中删除观察者时,应用程序有时会崩溃 - App crashes sometimes when removing observer from AVPlayer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM