Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/101.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 我是否应该删除NSUbiquitousKeyValueStoreDidChangeExternallyNotification的通知_Ios_Icloud_Nsnotificationcenter - Fatal编程技术网

Ios 我是否应该删除NSUbiquitousKeyValueStoreDidChangeExternallyNotification的通知

Ios 我是否应该删除NSUbiquitousKeyValueStoreDidChangeExternallyNotification的通知,ios,icloud,nsnotificationcenter,Ios,Icloud,Nsnotificationcenter,我正在使用本教程开始: 我已经使用它,实现它和测试,一切都很好 在这里,我发现通知应该只在dealloc中删除(在文章末尾) 我的问题是我是否应该删除NSUbiquitousKeyValueStoreDidChangeExternallyNotification的通知? 用什么方法?通常,对于任何通知,当您不再关心通知时,停止观察。您不再关心的一种情况是,观察通知的对象被解除分配 第二篇文章告诉您的是,removeObserver:通常不是最佳选择。它将从所有通知中删除观察者。这包括您注册的

我正在使用本教程开始:

我已经使用它,实现它和测试,一切都很好

在这里,我发现通知应该只在dealloc中删除(在文章末尾)

我的问题是我是否应该删除NSUbiquitousKeyValueStoreDidChangeExternallyNotification的通知?
用什么方法?

通常,对于任何通知,当您不再关心通知时,停止观察。您不再关心的一种情况是,观察通知的对象被解除分配

第二篇文章告诉您的是,
removeObserver:
通常不是最佳选择。它将从所有通知中删除观察者。这包括您注册的通知以及框架内部使用的通知。正如本文所解释的,最好使用
removeObserver:name:object:
显式注销每个通知。它并不是告诉您只应在
dealloc
中注销通知,而是告诉您只应使用dealloc中的
removeObserver:
方法注销通知。这是一个很好的建议,但是在需要使用其他remove-observer方法的地方使用它们是可以的


这通常意味着您希望在
dealloc
中注销,但这实际上取决于您的代码的功能以及您需要接收通知的时间。

我的困惑在于此。如果我需要在dealloc中删除它,根据我的“业务逻辑”,我为什么要这样做?因为dealloc之后就没有我的对象了,所以通知无法到达它,那么为什么要麻烦首先删除它呢?请您为我澄清一下,这是因为您想告诉
NSNotificationCenter
,它不应该再尝试向对象发送通知。你这么做是因为你不想让你的应用程序崩溃
NSNotificationCenter
可能足够聪明来解决这个问题,但是如果您想要稳定的代码,您应该确保。