NgNotificationProxy是一个小巧的Objective-C库,它提供了与NSNotificationCenter交互的更好方式。
如果您正在使用Cocoapods,请将其添加到您的Podfile中
pod NgNotificationProxy手动添加到您的项目中
NgWeakProxy.h、NgWeakProxy.m、NgWeakProxySubclass.h、NgNotificationProxy.h和NgNotificationProxy.m添加到您的项目中。NgNotificationProxy需要ARC。
NgNotificationProxy旨在使向特定线程发送通知变得简单。请参阅向特定线程发送通知。
此外,NgNotificationProxy还会检测观察者引用是否无效,并自动停止观察通知(而不是崩溃)。
使用NgNotificationProxy,您可以指定接收通知的目标线程
NgNotificationProxyThreadCurrent,将通知发送到与它发布的同一线程。若未指定,则为默认线程。NgNotificationProxyThreadMain,将通知发送到主线程。NgNotificationProxyThreadBackground,将通知发送到NgNotificationProxy的后台线程(线程名:NgNotificationProxy.internal.background)。向当前线程发送通知。
[[NgNotificationProxy defaultProxy] addObserver:self 
                                       selector:@selector(onNotification:) 
                                           name:@"NotificationName" 
                                          object:nil];向主线程发送通知。
[[NgNotificationProxy defaultProxy] addObserver:self 
                                       selector:@selector(onNotification:) 
                                           name:@"NotificationName" 
                                         object:nil 
                                         thread:NgNotificationProxyThreadMain];向后台线程发送通知。
[[NgNotificationProxy defaultProxy] addObserver:self 
                                       selector:@selector(onNotification:) 
                                           name:@"NotificationName" 
                                         object:nil 
                                         thread:NgNotificationProxyThreadBackground];向用户定义的线程发送通知。
[[NgNotificationProxy defaultProxy] addObserver:self 
                                       selector:@selector(onNotification:) 
                                           name:@"NotificationName" 
                                         object:nil 
                                     threadName:@"MyThread"];停止观察通知。
[[NgNotificationProxy defaultProxy] removeObserver:self];或者
[[NgNotificationProxy defaultProxy] removeObserver:self 
                                              name:@"NotificationName" 
                                            object:nil];NgNotificationProxy让您拥有更好的控制权,并编写更干净的代码。最常见的用例之一是,当事件发生后将通知发送到主线程以执行UI更新。
使用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];
  });
}使用NgNotificationProxy.
// observing notification
[[NgNotificationProxy defaultProxy] addObserver:self
                                       selector:@selector(updateUI:) 
                                           name:kUIDataUpdateNotification 
                                         object:nil
                                         thread:NgNotificationProxyThreadMain];
// updateUI implementation
- (void)updateUI:(NSNotification *)notification
{
  [self reloadData];
}