Frage

I am using this tutorial to get started:
http://www.raywenderlich.com/63269/how-to-save-your-game-data-tutorial-part-2-of-2

I have use it, implemented it and test and everything is working fine.

Here I found that notification should be removed only in dealloc (at the end of the article)
http://subjective-objective-c.blogspot.com/2011/04/avoid-nsnotification-removeobserver.html

My question is should I remove notification for NSUbiquitousKeyValueStoreDidChangeExternallyNotification? And in what method ?

War es hilfreich?

Lösung

In general, for any notification, stop observing when you no longer care about the notification. One case where you don't care any more is when the object that is observing the notification gets deallocated.

What that second article is telling you is that removeObserver: is often not the best choice. It removes the observer from all notifications. That includes notifications that you registered for as well as notifications that the frameworks use internally. As the article explains, it's better to use removeObserver:name:object: to explicitly unregister for each notification. It's not telling you that you should only unregister for notifications in dealloc, it's telling you that you should only unregister for notifications using the removeObserver: method in dealloc. That's good advice, but it's OK to use the other remove-observer methods wherever you need to use them.

That usually means you want to unregister in dealloc, but it really depends on what your code does and on when you need to receive the notifications.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top