gpt4 book ai didi

cocoa - NSNotificationCenter 为 'observer' 和 'object' 保留什么类型的引用?

转载 作者:行者123 更新时间:2023-12-03 16:01:58 24 4
gpt4 key购买 nike

任何人都可以澄清/阐明有关 -[NSNotificationCenter addObserver:selector:name:object:] 的情况吗?

  • “观察者”和“对象”参数的通知中心保留哪些类型的引用?

  • 从通知中心删除观察者的最佳做法是什么?

  • 多线程应用程序有哪些特殊问题,尤其是与“对象”参数相关的问题?

  • 此方法在 GC 和非 GC 环境中的行为有何差异?

  • 此方法的行为在移动环境和桌面环境之间是否存在显着差异(从客户端角度来看)?

此外,如果您能指出涵盖此内容的现有文章,我们将不胜感激。我在 Google 上搜索,但惊讶地发现对这些问题的深入讨论很少(尽管也许我没有使用正确的魔术关键字)。

最佳答案

what types of references are kept by the notification center of the 'observer' and 'object' arguments?

我相信弱引用,尽管这只是凭内存(没有双关语)。

what are the best practices for removing observers from the notification center?

在释放注册的对象之前,请务必将其从通知中心删除。如果该对象的 dealloc 方法本身设置了注册,或者当另一个对象正在管理通知订阅时释放它,则该对象的 dealloc 方法是一个好地方。记住这一点,上面的内容就不再重要了。

what are the special concerns for multi-threaded applications, especially WRT the 'object' argument?

NSNotificationCenter 在线程上工作正常,但是如果您从后台线程发送通知,则该对象将在同一线程上接收它。由于这种行为,如果您要更新 UI 或执行任何其他非线程安全的操作(或者从主线程上的另一个方法分派(dispatch)通知),您应该使用不同的方法。

what are the differences in behavior of this method in GC and non-GC environments?

我不记得听说过任何您需要担心的事情,尽管我还没有太多使用 GC。

are the any significant differences (from a client perspective) between mobile and desktop environments in this method's behavior?

据我所知,没有。注册对象时,您可以选择注册所有通知或仅注册来自特定对象的通知。如果您大量使用通知,后者可能会更快一些,但请务必进行测试以确保确定。

Also, any pointers to existing articles which cover this would be greatly appreciated. I googled, but was surprised to find little in-depth discussion of these issues (although maybe i didn't use the right magic keywords).

我认为这更多是因为 NSNotificationCenter 总体来说非常容易使用。如果您担心某些情况,请不要害怕编写一个快速测试应用程序!

关于cocoa - NSNotificationCenter 为 'observer' 和 'object' 保留什么类型的引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/425354/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com