TestsTested | ✗ |
LangLanguage | Obj-CObjective C |
License | zlib |
ReleasedLast Release | Apr 2016 |
Maintained by Nick Lockwood.
FXNotifications is a category on NSNotificationCenter
that provides an improved block-based API that is simpler and easier to use, and avoids the various retain cycle and memory leak pitfalls of the official API.
For more details, see this article: http://sealedabstract.com/code/nsnotificationcenter-with-blocks-considered-harmful/ and this gist: https://gist.github.com/nicklockwood/7559729
NOTE: 'Supported' means that the library has been tested with this version. 'Compatible' means that the library should work on this iOS version (i.e. it doesn't rely on any unavailable SDK features) but is no longer being tested for compatibility and may require tweaking or bug fixes to run correctly.
FXNotifications requires ARC and uses weak references.
It is safe to add and remove observers concurrently on different threads using the FXNotification methods. Callback blocks will be executed on the specified queue.
To use FXNotifications, just drag the FXNotifications.h and .m files into your project and import the header file in your class.
FXNotifications extends NSNotificationCenter with a single method
- (id)addObserver:(id)observer
forName:(NSString *)name
object:(id)object
queue:(NSOperationQueue *)queue
usingBlock:(void (^)(NSNotification *note, id observer))block;
This method is a hybrid of the two built-in notification observer methods. The observer parameter is required, and represents the owner of the block argument. When the observer is released, the block will be released as well.
The name, object, queue and block arguments work as they do in the normal block-based observer method. The queue parameter is optional - if nil is passed then the block will be executed on whichever queue the notification is posted from. To avoid retain cycles in your block, you can refer to the weak observer parameter that is passed as a second argument.
The method returns a token value that can be used to stop observing the notification, use the standard -removeObserver:
method of NSNotificationCenter
, e.g.
[[NSNotificationCenter defaultCenter] removeObserver:token];
However, you can simply discard this token and the token will be deregistered automatically when the observer is released. There is no need to call removeObserver: in the observer's dealloc method; this is done automatically.
Also, if you wish, you can deregister the observer itself using the -removeObserver:
or -removeObserver:name:object:
methods of NSNotificationCenter
, so the only reason to store the token is if you wish to distinguish between multiple registrations of the same observer with the same selector.
A typical usage might be:
[[NSNotificationCenter defaultCenter] addObserver:self
forName:NSSomeNotificationName
object:nil
queue:[NSOperationQueue mainQueue]
usingBlock:^(NSNotification *note, id observer) {
NSLog(@"self: %@", observer); // look, no leaks!
}];
Version 1.1
Version 1.0.2
Version 1.0.1
Version 1.0