TestsTested | ✓ |
LangLanguage | Obj-CObjective C |
License | MIT |
ReleasedLast Release | Jan 2016 |
Maintained by Meiwin Fu.
NgNotificationProxy is small objective-c library that provides better way to work with notifications from NSNotificationCenter
.
If you are using Cocoapods, add to your Podfile:
pod NgNotificationProxy
To manually add to your projects:
NgWeakProxy.h
, NgWeakProxy.m
, NgWeakProxySubclass.h
, NgNotificationProxy.h
and NgNotificationProxy.m
to your project.NgNotificationProxy requires ARC.
NgNotificationProxy is built to make delivering notifications to particular threads a trivial task. See Delivering Notifications to Particular Threads.
In addition, NgNotificationProxy also detects if an observer reference is invalid and stop observing for notifications automatically (instead of crashing).
With NgNotificationProxy, you can specify target thread to which notification is delivered:
NgNotificationProxyThreadCurrent
, deliver notifications to the same thread as where it is posted. This is default thread if not specified.NgNotificationProxyThreadMain
, deliver notifications to main thread.NgNotificationProxyThreadBackground
, deliver notifications to NgNotificationProxy's background thread (thread name: NgNotificationProxy.internal.background
).Deliver notifications to current thread.
[[NgNotificationProxy defaultProxy] addObserver:self
selector:@selector(onNotification:)
name:@"NotificationName"
object:nil];
Deliver notifications to main thread.
[[NgNotificationProxy defaultProxy] addObserver:self
selector:@selector(onNotification:)
name:@"NotificationName"
object:nil
thread:NgNotificationProxyThreadMain];
Deliver notifications to background thread.
[[NgNotificationProxy defaultProxy] addObserver:self
selector:@selector(onNotification:)
name:@"NotificationName"
object:nil
thread:NgNotificationProxyThreadBackground];
Deliver notifications to user-defined thread.
[[NgNotificationProxy defaultProxy] addObserver:self
selector:@selector(onNotification:)
name:@"NotificationName"
object:nil
threadName:@"MyThread"];
Stop observing notifications.
[[NgNotificationProxy defaultProxy] removeObserver:self];
or
[[NgNotificationProxy defaultProxy] removeObserver:self
name:@"NotificationName"
object:nil];
NgNotificationProxy gives you better control and writing cleaner code. One of the most common use case is to deliver notification to main thread for performing UI updates as result of an event.
With NSNotificationCenter
.
// observing notification
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(updateUI:)
name:kUIDataUpdateNotification
object:nil];
// updateUI implementation.
- (void)updateUI:(NSNotification *)notification
{
dispatch_async(dispatch_get_main_queue(), ^{ // ensure `reloadData` always execute in main-thread
[self reloadData];
});
}
With NgNotificationProxy
.
// observing notification
[[NgNotificationProxy defaultProxy] addObserver:self
selector:@selector(updateUI:)
name:kUIDataUpdateNotification
object:nil
thread:NgNotificationProxyThreadMain];
// updateUI implementation
- (void)updateUI:(NSNotification *)notification
{
[self reloadData];
}