简体   繁体   English

Swift NotificationCenter 删除观察者最快的方法

[英]Swift NotificationCenter remove observer quickest way

I am adding a number of observers in my viewController -- applicationWillResignActive , applicationDidEnterBackground , and many others.我在我的viewController添加了许多观察者—— applicationWillResignActiveapplicationDidEnterBackground等等。 I want to remove self as observer to all registered notifications in one line.我想在一行中将self作为观察者删除到所有已注册的通知中。 My question is whether the following line is enough to do that, or are there issues with this code?我的问题是以下行是否足以做到这一点,或者这段代码有问题吗?

deinit {
   NotificationCenter.default.removeObserver(self)
}

@Sh_Khan is right: @Sh_Khan 是对的:

NotificationCenter.default.removeObserver(self)

You can get even further, as mentioned in the Apple Documentation :您可以进一步了解,如Apple 文档中所述

If your app targets iOS 9.0 and later or macOS 10.11 and later, you don't need to unregister an observer in its dealloc method.如果您的应用面向 iOS 9.0 及更高版本或 macOS 10.11 及更高版本,则无需在它的 dealloc 方法中取消注册观察者。

So I'm working with this in an app right now and the answer might not be as straightforward.所以我现在正在一个应用程序中处理这个问题,答案可能并不那么简单。

In the documentation, it does state that for iOS 9 and above you are no longer required to explicitly remove the observer in the deinit/dealloc methods for objects.在文档中,它确实声明对于 iOS 9 及更高版本,您不再需要在对象的 deinit/dealloc 方法中显式删除观察者。 https://developer.apple.com/documentation/foundation/notificationcenter/1413994-removeobserver https://developer.apple.com/documentation/foundation/notificationcenter/1413994-removeobserver

However, it appears that this is only true for selector based notification observers.然而,这似乎只适用于基于选择器的通知观察者。 I'll be referencing this blog post: https://oleb.net/blog/2018/01/notificationcenter-removeobserver/ .我将参考这篇博文: https : //oleb.net/blog/2018/01/notificationcenter-removeobserver/

If you are using block based observers you must still manually remove the observers.如果您使用基于块的观察者,您仍然必须手动删除观察者。

addObserver(forName:object:queue:using:) 

The best general way to do this is to capture the tokens in an array, append them when you add the observer and use them for removal when you deinit/dealloc or otherwise need to remove observer behavior for your object.最好的通用方法是捕获数组中的标记,在添加观察者时附加它们,并在 deinit/dealloc 或其他需要删除对象的观察者行为时使用它们进行删除。

in your VC/object properties create an array to store observer 'tokens'在您的 VC/对象属性中创建一个数组来存储观察者“令牌”

var notifObservers = [NSObjectProtocol]()

Register for a block based notification by capturing the function return object and storing it as a token通过捕获函数返回对象并将其存储为令牌来注册基于块的通知

let observer = NotificationCenter.default.addObserver(forName: , object: , queue:) { [weak self] notification in
    // do a thing here
}
notifObservers.append(observer)

Removal移动

for observer in notifObservers {
    NotificationCenter.default.removeObserver(observer)
}
notifObservers.removeAll()

Yes是的

NotificationCenter.default.removeObserver(self)

this line is sufficient to remove the vc observation as long as all are added with这条线足以去除 vc 观察,只要全部添加

NotificationCenter.default.addObserver

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM