观察者变为零时,观察者会自动删除吗? [英] Will observers automatically removed when observers become nil?

查看:98
本文介绍了观察者变为零时,观察者会自动删除吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在viewDidLoad中使用addObserver:selector:name:object:.

我正在使用viewWillDisappear:animated:中的removeObserver:name:object:删除观察者.

And I'm using removeObserver:name:object: in viewWillDisappear:animated: to remove observer.

如果我无法通过将错误的参数传递给removeObserver:name:object:来删除观察者,将会发生什么?
(例如,如果我将错误的通知传递给参数name或将错误的对象传递给objectObserver,则不会删除观察者)

What will happen if I failed to remove observer by passing wrong parameter to removeObserver:name:object:?
(For example, observer isn't removed if I pass wrong notification to parameter name or wrong object to object or Observer)

如果在调用removeObserver:name:object:之后观察者仍然不为零,我可以发现删除观察者失败,因为将调用notifySelector.

If the observer still not nil after calling removeObserver:name:object:, I can find out that removing observer failed because notificationSelector will being called.

但是如果观察者在调用removeObserver:name:object:之后变为nil,我将无法确定移除观察者是否失败.

But if the observer become nil after calling removeObserver:name:object:, I can not find out whether removing observer failed or not.

观察者变为零时,观察者会自动删除吗?
还是NSNotificationCenter中的notification dispatch table变得越来越大,最终导致该应用变慢?

Will observers automatically removed when observer become nil?
Or does notification dispatch table of NSNotificationCenter became larger and larger and eventually the app become slow?

编辑

当我使用UIViewController对象的子类作为观察者时,调用ViewController的dealloc后应用程序不会崩溃.
但是当我使用其他类的对象时,调用该对象的dealloc后,应用程序崩溃.

When I use subclass of UIViewController object for observer, the app doesn't crash after ViewController's dealloc are called.
But when I use a object of other class, the app crashs after the object's dealloc are called.

推荐答案

更新:来自

如果您的应用程序针对iOS 9.0及更高版本或macOS 10.11及更高版本,则无需在其dealloc方法中注销观察者.否则,应在释放观察者或addObserverForName:object:queue:usingBlock:addObserver:selector:name:object:中指定的任何对象之前调用此方法或removeObserver:name:object:.

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. Otherwise, you should call this method or removeObserver:name:object: before observer or any object specified in addObserverForName:object:queue:usingBlock: or addObserver:selector:name:object: is deallocated.

旧答案:

观察者不会自动删除.从NSNotificationCenter类参考中:

Observers are not removed automatically. From the NSNotificationCenter Class Reference:

重要提示:通知中心没有保留其观察者, 因此,您必须确保取消注册观察者(使用 removeObserver:或removeObserver:name:object :) 释放. (如果不这样做,如果 中心向已释放的对象发送消息.)

Important: The notification center does not retain its observers, therefore, you must ensure that you unregister observers (using removeObserver: or removeObserver:name:object:) before they are deallocated. (If you don't, you will generate a runtime error if the center sends a message to a freed object.)

因此,您应该致电

[[NSNotificationCenter defaultCenter] removeObserver:self];

如果您不是100%不确定先前没有删除观察者,请在dealloc方法中输入

.

in your dealloc method if you are not 100% sure that the observer was not removed previously.

这篇关于观察者变为零时,观察者会自动删除吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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