Objective-C:在哪里删除NSNotification的观察者? [英] Objective-C: Where to remove observer for NSNotification?

查看:87
本文介绍了Objective-C:在哪里删除NSNotification的观察者?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个客观的C类.在其中,我创建了一个init方法并在其中设置了NSNotification

I have an objective C class. In it, I created a init method and set up a NSNotification in it

//Set up NSNotification
[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(getData)
                                             name:@"Answer Submitted"
                                           object:nil];

在该课程的哪里设置[[NSNotificationCenter defaultCenter] removeObserver:self]?我知道对于UIViewController,我可以将其添加到viewDidUnload方法中,那么,如果我刚刚创建了目标c类,该怎么办?

Where do I set the [[NSNotificationCenter defaultCenter] removeObserver:self] in this class? I know that for a UIViewController, I can add it into the viewDidUnload method So what needs to be done if I just created an objective c Class?

推荐答案

通用答案是只要您不再需要通知".这显然不是令人满意的答案.

The generic answer would be "as soon as you no longer need the notifications". This is obviously not a satisfying answer.

我建议您在这些类的方法dealloc中添加一个调用[notificationCenter removeObserver: self],您打算将其用作观察者,因为这是彻底注销观察者的最后机会.但是,这只会保护您免于因通知中心通知死物而导致的崩溃.当对象尚未/不再处于可以正确处理通知的状态时,它不能保护代码不接收通知.为此...请参见上文.

I'd recommend, that you add a call [notificationCenter removeObserver: self] in method dealloc of those classes, which you intend to use as observers, as it is the last chance to unregister an observer cleanly. This will, however, only protect you against crashes due to the notification center notifying dead objects. It cannot protect your code against receiving notifications, when your objects are not yet/no longer in a state in which they can properly handle the notification. For this... See above.

编辑(因为答案似乎比我想的要多得多).我在这里要说的是:很难就何时最佳删除建议给出一般性建议.通知中心的观察员,因为这取决于:

Edit (since the answer seems to draw more comments than I would have thought) All I am trying to say here is: it's really hard to give general advice as to when it's best to remove the observer from the notification center, because that depends:

  • 关于您的用例(观察到哪些通知?何时发送通知?)
  • 观察者的实现(何时准备接收通知?何时不再准备接收通知?)
  • 观察者的预期寿命(它是否绑定到其他对象,例如视图或视图控制器?)
  • ...

因此,我能提供的最佳一般建议是:保护您的应用程序.对于至少一个可能的失败,请在dealloc中执行removeObserver:舞步,因为这是(对象生命中的)最后一点,您可以在其中进行整洁的操作.这并不意味着:只需将删除操作推迟到dealloc被调用,一切都会好起来的".取而代之的是,在对象不再准备好(或不需要)接收通知时,移除观察者.那是正确的时刻.不幸的是,不知道上面提到的任何问题的答案,我什至无法猜测,那一刻将是什么时候.

So, the best general advice I can come up with: to protect your app. against at least one possible failure, do the removeObserver: dance in dealloc, since that's the last point (in the object's life), where you can do that cleanly. What this does not mean is: "just defer the removal until dealloc is called, and everything will be fine". Instead, remove the observer as soon as the object is no longer ready (or required) to receive notifications. That is the exact right moment. Unfortunately, not knowing the answers to any of the questions mentioned above, I cannot even guess, when that moment would be.

您始终可以多次安全地removeObserver:一个对象(除了与给定观察者的第一个调用之外的所有调用都是nops).因此,请确保再次考虑在dealloc中进行此操作,但首先要注意的是:在适当的时候进行此操作(这由您的用例确定).

You can always safely removeObserver: an object multiple times (and all but the very first call with a given observer will be nops). So: think about doing it (again) in dealloc just to be sure, but first and foremost: do it at the appropriate moment (which is determined by your use case).

这篇关于Objective-C:在哪里删除NSNotification的观察者?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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