关于iOS 9之后通知的移除

在 iOS 9 之后,不需要再在 dealloc 方法中去移除通过 addObserver:selector:name:object: 方法注册的通知的监听。 但是通过 addObserverForName:object:queue:usingBlock: 这个方法注册的通知仍然需要手动移除。

参考文档:

image.png

文档链接:https://developer.apple.com/documentation/foundation/nsnotificationcenter/1407263-removeobserver?language=objc

个人理解, iOS 9 之后自动移除通知的实现是,NSNotificationCenter 对 observer 为弱引用,当observer 释放掉的之后,下次 NSNotificationCenter 向 observer 发通知时,就会发现 observer 已经释放掉了,会自动为 observer 移除通知。

而通过 addObserverForName:object:queue:usingBlock: 这个方法注册的通知仍然是强引用。(因为没有指定 observer 吗? )

查到的相关信息:

image.png

截图链接:https://useyourloaf.com/blog/unregistering-nsnotificationcenter-observers-in-ios-9/

你可能感兴趣的:(关于iOS 9之后通知的移除)