Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rule request: Discarded opaque NSNotificationCenter observer #1062

Closed
marcelofabri opened this issue Dec 25, 2016 · 5 comments
Closed

Rule request: Discarded opaque NSNotificationCenter observer #1062

marcelofabri opened this issue Dec 25, 2016 · 5 comments
Labels
rule-request Requests for a new rules.

Comments

@marcelofabri
Copy link
Collaborator

From http://fauxpasapp.com/rules/#rule-DiscardedOpaqueNotificationObserver:


Warns if the return value of -[NSNotificationCenter addObserverForName:object:queue:usingBlock:] is not stored anywhere. This method returns an opaque observer object that is needed in order to stop observing.

@jshier
Copy link
Contributor

jshier commented Mar 11, 2017

Is this still necessary with the automatic unobserving in iOS 9 and later?

@marcelofabri
Copy link
Collaborator Author

Yes, because that only works when using the selector-based methods.

@bimawa
Copy link

bimawa commented Nov 7, 2018

Guys, what do you think about this research https://oleb.net/blog/2018/01/notificationcenter-removeobserver/ ?

@daltyboy11
Copy link

daltyboy11 commented Nov 6, 2019

@marcelofabri That's not how I interpret the documentation

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.

The first sentence implies after iOS 9.0 you don't need to explicitly unregister the observer no matter the method used. Where did you learn that automatic unregister only works for selector-based methods?

@jshier
Copy link
Contributor

jshier commented Nov 6, 2019

@daltyboy11 It's trivially confirmable experimentally, as done in the blog post linked just above you by @bimawa. The documentation for addObserver(forName:object:queue:using:) is also pretty clear:

You must invoke removeObserver(:) or removeObserver(:name:object:) before any object specified by addObserver(forName:object:queue:using:) is deallocated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rule-request Requests for a new rules.
Projects
None yet
Development

No branches or pull requests

4 participants