addObserverForName这个方法并不常用, 但估计很多人和我一样, 最开始的时候也不太了解这个方法, 再看了网上的一些技术贴, 更是对addObserverForName产生了误解.
应用场景
当通知的发出不在主线程, 但是发出的通知需要更新UI, 就可能会用到这个方法.
- (id <NSObject>)addObserverForName:(nullable NSNotificationName)name
object:(nullable id)obj
queue:(nullable NSOperationQueue *)queue
usingBlock:(void (^)(NSNotification *note))block NS_AVAILABLE(10_6, 4_0);
// The return value is retained by the system, and should be held onto by the caller in
// order to remove the observer with removeObserver: later, to stop observation.
这里我把苹果爸爸的注释也copy上了, 意思, 是使用者要持有addObserverForName的返回值, 也就是持有这个observer, 后面需要通过removeObserver来停止监听. 但实际使用中, 我们会发现, addObserverForName并不需要caller去持有这个observer, 只需要在block中使用weak, 系统就会在dealloc中帮我们去remove这个observer.
没想到几年前的一篇学习心得会给大家造成这么大的困扰,在此表示表示歉意,上面的这种说法是不严谨的,不是因为我们使用了weak系统才帮我们去remove,而且从iOS9开始,当对象dealloc的时候,系统会帮我执行removeObserver
, 所以这里使用weak是为了不让block对self产生强引用,进而产生循环引用,即系统(NSNotification
内部持有block),block持有self,self不释放,系统block也不会触发释放
由于有人质疑此文是拿来主义,为此本人还亲自重新做了实验,与当时的结果还是一样的,这里把实验的细节也放上来,也是当时做实验的一些心路历程。
实验1:block中使用self, 在不适用后进行移除
结论1:没有用, self没法释放, dealloc不会执行, 但观察者可以成功移除,beginLuckyDogs 只会执行一次
- (void)dealloc
{
NSLog(@"begin dealloc");
[[NSNotificationCenter defaultCenter] removeObserver:self.observer name:@"luckyDogNotification" object:nil];
}
- (void)beginLuckyDogs {
NSLog(@"beginLuckyDogs=========");
}
- (void)viewDidLoad {
[super viewDidLoad];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:@"luckyDogNotification" object:nil];
});
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(6 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSLog(@"luckyDogNotification");
[[NSNotificationCenter defaultCenter] removeObserver:self.observer name:@"luckyDogNotification" object:nil];
});
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(9 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:@"luckyDogNotification" object:nil];
});
self.observer = [[NSNotificationCenter defaultCenter] addObserverForName:@"luckyDogNotification"
object:nil
queue:[NSOperationQueue mainQueue]
usingBlock:^(NSNotification *note) {
[self beginLuckyDogs];
}];
}
实验2:同实验1,只是在移除观察的时候用[[NSNotificationCenter defaultCenter] removeObserver:self];
结论2:没有用,观察无法移除成功,self无法释放
- (void)dealloc
{
NSLog(@"begin dealloc");
[[NSNotificationCenter defaultCenter] removeObserver:self.observer name:@"luckyDogNotification" object:nil];
}
- (void)beginLuckyDogs {
NSLog(@"beginLuckyDogs=========");
}
- (void)viewDidLoad {
[super viewDidLoad];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:@"luckyDogNotification" object:nil];
});
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(6 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSLog(@"luckyDogNotification");
[[NSNotificationCenter defaultCenter] removeObserver:self];
});
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(12 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:@"luckyDogNotification" object:nil];
});
[[NSNotificationCenter defaultCenter] addObserverForName:@"luckyDogNotification"
object:nil
queue:[NSOperationQueue mainQueue]
usingBlock:^(NSNotification *note) {
[self beginLuckyDogs];
}];
实验3:类似实验2,只是block中使用weak
结论3:self可以释放,但观察者[[NSNotificationCenter defaultCenter] removeObserver:self];不能正常移除成功,beginLuckyDogs会执行多次
- (void)dealloc
{
NSLog(@"begin dealloc");
[[NSNotificationCenter defaultCenter] removeObserver:self.observer name:@"luckyDogNotification" object:nil];
}
- (void)beginLuckyDogs {
NSLog(@"beginLuckyDogs=========");
}
- (void)viewDidLoad {
[super viewDidLoad];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:@"luckyDogNotification" object:nil];
});
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(6 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSLog(@"luckyDogNotification");
[[NSNotificationCenter defaultCenter] removeObserver:self];
});
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(12 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:@"luckyDogNotification" object:nil];
});
__weak typeof(self) weak_self = self;
[[NSNotificationCenter defaultCenter] addObserverForName:@"luckyDogNotification"
object:nil
queue:[NSOperationQueue mainQueue]
usingBlock:^(NSNotification *note) {
[weak_self beginLuckyDogs];
}];
}
实验4:block中使用weak, 不显式调用remove操作
结论4:self可以正常释放,不会多次执行beginLuckyDogs
- (void)dealloc
{
NSLog(@"begin dealloc");
}
- (void)beginLuckyDogs {
NSLog(@"beginLuckyDogs=========");
}
- (void)viewDidLoad {
[super viewDidLoad];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:@"luckyDogNotification" object:nil];
});
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(9 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:@"luckyDogNotification" object:nil];
});
__weak typeof(self) weak_self = self;
[[NSNotificationCenter defaultCenter] addObserverForName:@"luckyDogNotification"
object:nil
queue:[NSOperationQueue mainQueue]
usingBlock:^(NSNotification *note) {
[weak_self beginLuckyDogs];
}];
}
由于之前的文章,少了一些中间的心路历程,造成了大家的误解,评论区有说学艺不精
,误人子弟
的都欣然接受。
有些同学可能并不知道系统会帮我们remove这个情况, 还是会在dealloc中去无脑调用一次[[NSNotificationCenter defaultCenter] removeObserver:self];
但是我们应该知道[[NSNotificationCenter defaultCenter] removeObserver:self];
应该是用于普通的addObserver
的移除的.
上面这段描述也不够准确,针对iOS9系统才会不移自除,对于非iOS9,还是要用
- (void)removeObserver:(id)observer name:(nullable NSNotificationName)aName object:(nullable id)anObject;
进行移除,[[NSNotificationCenter defaultCenter] removeObserver:self];
无法移除- (id <NSObject>)addObserverForName:(nullable NSNotificationName)name object:(nullable id)obj queue:(nullable NSOperationQueue *)queue usingBlock:(void (^)(NSNotification *note))block API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0));
方式添加的观察者
从iOS9以后,确实在对下释放的时候系统会帮我们进行remove操作,本文的核心意思主要是针对
- (id <NSObject>)addObserverForName:(nullable NSNotificationName)name
object:(nullable id)obj
queue:(nullable NSOperationQueue *)queue
usingBlock:(void (^)(NSNotification *note))block NS_AVAILABLE(10_6, 4_0);
// The return value is retained by the system, and should be held onto by the caller in
// order to remove the observer with removeObserver: later, to stop observation.
这段代码中,苹果的这个注释展开,并结合使用中出现强引用问题造成对象无法释放问题的调查。