您的位置:首页 > 其它

NSNotification系统通知优化

2016-07-11 17:55 204 查看
最近在github上看到了LRNotificationObserver这个项目,看了一下实现方式,作者通过ARC机制实例化注册对象子类与关联对象的方法来管理注册对象的生命周期。从而省去了系统通知移除的过程,本篇介绍该项目实现过程。

NSNotificationCenter 通知存在的问题

注册

[[NSNotificationCenter defaultCenter] addObserver:anObserver
selector:@selector(handleNotification:)
name:aNotificationName
object:anObjectThatFiresTheNotification];


移除

[[NSNotificationCenter defaultCenter] removeObserver:anObserver
name:aNotificationName
object:anObjectThatFiresTheNotification];


这是正常使用通知的情形,这样使用存在两个问题

每次使用都需要手动移除观察者。

如果在主线程注册观察者然后在线程B中发送通知,如果通知事件是更新UI那么就会出现问题因为此时是在线程B操作而不是主线程。

LRNotificationObserver

LRNotificationObserver
对象的内部持有
NSNotificationCenter
对象实际上是对
NSNotificationCenter
基础功能进一步的封装。

LRNotificationObserver自动管理观察者

LRNotificationObserver
有两种方法初始化

实例化注册对象

@property (nonatomic, strong) LRNotificationObserver *backgroundObserver;

self.backgroundObserver = [LRNotificationObserver observerForName:UIApplicationDidEnterBackgroundNotification
block:^(NSNotification *note) {
// Do appropriate background task
}];


这种方法需要声明一个
LRNotificationObserver
对象作为属性,根据ARC内存管理当对象销毁时对象的属性也会一起销毁

这种情况下作者把销毁的方法写在了
LRNotificationObserver
对象的
dealloc
方法中。

- (void)dealloc
{
[self stopObserving];
}
- (void)stopObserving
{
[_notificationCenter removeObserver:self name:_name object:_object];
[self clear];
}
- (void)clear
{
_block = nil;
_targetAction = nil;
_operationQueue = nil;
_dispatchQueue = nil;
}


使用类方法不用实例化对象

[LRNotificationObserver observeName:UIApplicationDidReceiveMemoryWarningNotification
owner:self
block:^(NSNotification *note) {
// Purge unnecessary cache
}];


虽然不用实例化
LRNotificationObserver
但是要传入
owner
参数,
owner
相当于一个钩子,让其与
LRNotificationObserver
对象进行关联。

+ (void)addObserver:(LRNotificationObserver *)observer asPropertyOfOwner:(id)owner
{
objc_setAssociatedObject(owner, (__bridge void *)observer, observer, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}


通过运行时关联对象的方法将
LRNotificationObserver
对象与
owner
对象关联,当
owner
销毁时关联断开,
LRNotificationObserver
对象不被任何对象持有销毁。执行
dealloc
方法

线程安全

LRNotificationObserver
可以指定通知方法执行队列

[LRNotificationObserver observeName:@"someNotificationThatShouldUpdateTheUI"
object:anObject
owner:anOwner
dispatch_queue:dispatch_get_main_queue()
target:viewController
selector:@selector(methodToBeExecutedOnMainThread:)];


当收到通知后线程会自动切换到
dispatch_queue
置顶队列所在的线程中。

- (void)executeNotificationFiredBlock:(dispatch_block_t)block
{
if (self.operationQueue)
{
if ([NSThread isMainThread] && self.operationQueue == [NSOperationQueue mainQueue])
{
block();
}
else
{
[self.operationQueue addOperationWithBlock:block];
}
}
else if (self.dispatchQueue)
{
if ([NSThread isMainThread] && self.dispatchQueue == dispatch_get_main_queue())
{
block();
}
else
{
dispatch_async(self.dispatchQueue, block);
}
}
else
{
block();
}
}


作者通过是否指定
dispatch_queue
operationQueue
来判断是否切换队列,如果没有指定那么默认操作将在发送通知的线程中执行。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: