Comments (1)
No longer needed. Use FXNotifications instead.
Quoting myself from earlier:
This solves two main problems with block based observers: 1) Inadvertently retaining self, and 2) Forgetting to remove the block based observation (which requires one to retain the returned observer object). The combination of 1 and 2 can lead to an even bigger memory leak where self is retained for the lifetime of the application. See [1] for more details.
So, for example, you could simply write this in init (if that’s where you want to observe the notification):
[[NSNotificationCenter defaultCenter] addObserver:self
forName:NSSomeNotificationName
object:nil
queue:[NSOperationQueue mainQueue]
usingBlock:^(NSNotification *note, id self) {
NSLog(@"self: %@", self); // look, no leaks!
}];
And be rest assured that the observation will go away when the “observer” is deallocated. It also returns an observation token just like the original API which can be used to manually remove the observation (e.g. when you need to observe/unobserve notifications in -viewWillAppear: and -viewDidAppear: respectively).
Also, because we shadow self in the observation block, there’s no danger of a leak (unless you reference an ivar directly!)
from chocolate.
Related Issues (9)
- +[NSDateFormatter sharedISO8601Formatter] is not safe HOT 1
- Can't run tests on OS X HOT 3
- Add assertion to ensure a method is called only once per instance? HOT 4
- Add NSPrintf HOT 1
- Why do we require [[NSProcessInfo processInfo] tdt_isBeingDebugged]? HOT 2
- Spread the ownership HOT 3
- Consider renaming tdt_stringAfterNillingBlanks HOT 3
- Category on NSArray though it should be defined on NSMutableArray
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from chocolate.