最近看到一段有趣的代碼
if ([[UIDevice currentDevice].systemVersion floatValue] < 9.0) {
__weak typeof(self) weakSelf;
[[NSNotificationCenter defaultCenter] addObserverForName:kMEChangeNotification
object:nil
queue:[NSOperationQueue mainQueue]
usingBlock:^(NSNotification * _Nonnull note) {
[weakSelf doSomeThing];
}];
} else {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doSomeThing) name:kMEChangeNotification object:nil];
}
一開(kāi)始看到就猜想難道不同的系統(tǒng)接受通知的姿勢(shì)不一樣?但是之前一直都在用addObserver
疹蛉,也沒(méi)出現(xiàn)過(guò)問(wèn)題啊活箕,感覺(jué)這串代碼是多此一舉,就沒(méi)在管可款。后來(lái)有一次因?yàn)闆](méi)有removeObserver
引發(fā)了崩潰育韩,才發(fā)現(xiàn)這串代碼是有深意的克蚂。最近整理了一下,留作筆記筋讨,因?yàn)檫@里面特別繞
規(guī)則只有一個(gè)埃叭,無(wú)論什么時(shí)候都不可以打破
規(guī)則:addObserverForName
的block
里面必須使用weakSelf
,否則會(huì)造成循環(huán)引用導(dǎo)致當(dāng)前類無(wú)法釋放
注意事項(xiàng)
- 注意事項(xiàng)一:
doSomeThing
里面如果要刷新UI悉罕,最好是回調(diào)主線程赤屋。
因?yàn)?code>通知的接收所在的線程是基于發(fā)送通知所在的線程
,如果通知是在主線程發(fā)出的,通知的接收也是在主線程壁袄,如果通知的發(fā)送是在子線程类早,通知的接收也是在子線程。也就是說(shuō)如果你不能保證你的通知一定是在主線程發(fā)送的嗜逻,就最好回到主線程刷新UI
-(void)doSomeThing
{
//do something
dispatch_async(dispatch_get_main_queue(), ^{
// UI
});
}
- 注意事項(xiàng)二:通知的移除要調(diào)用各自的移除方法
addObserverForName
和addObserver
通知的移除方法是不一樣的
addObserver
通知的移除方法是[[NSNotificationCenter defaultCenter]removeObserver:self]
,這是最常見(jiàn)的涩僻,然而對(duì)于addObserverForName
是沒(méi)有用的,
addObserverForName
通知的移除方法是在聲明的時(shí)候要記錄通知變量
@interface SecondViewController ()
{
id notificationObserver;
}
//或者
//@property (nonatomic, strong) id notificationObserver;
@end
__weak typeof(self) weakSelf;
notificationObserver = [[NSNotificationCenter defaultCenter]addObserverForName:@"MYNotificationCenter" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
NSLog(@"addObserverForName接收到通知");
[weakSelf doSomeThing];
}];
然后使用[[NSNotificationCenter defaultCenter]removeObserver:notificationObserver];
移除
打破概念特別注意事項(xiàng)
特別注意事項(xiàng):通知的移除不一定非要寫(xiě)
對(duì)于addObserverForName:
__weak typeof(self) weakSelf;
notificationObserver = [[NSNotificationCenter defaultCenter]addObserverForName:@"MYNotificationCenter" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
NSLog(@"addObserverForName接收到通知"); //如果沒(méi)有移除這個(gè)通知栈顷,每次接到通知時(shí)這個(gè)NSLog都會(huì)輸出
[weakSelf doSomeThing]; //如果沒(méi)有移除這個(gè)通知逆日,如果是直接使用self,則這個(gè)通知所在NSObject或者ViewController都不會(huì)釋放,如果是使用weakSelf萄凤,則這個(gè)通知所在NSObject或者ViewController都會(huì)釋放屏富,就不會(huì)再繼續(xù)調(diào)用doSomeThing方法
}];
對(duì)于addObserver:
這里要分ViewController和普通NSObject兩個(gè)說(shuō)起
ViewController:在調(diào)用ViewController的dealloc的時(shí)候,系統(tǒng)會(huì)調(diào)用[[NSNotificationCenter defaultCenter]removeObserver:self]
方法蛙卤,所以如果是在viewDidLoad中使用addObserver添加監(jiān)聽(tīng)者的話可以省掉移除狠半。當(dāng)然,如果是在viewWillAppear中添加的話颤难,那么就要在viewWillAppear中自己移除神年,而且
,最好是使用[[NSNotificationCenter defaultCenter] removeObserver:self name:@"test" object:nil];
而非[[NSNotificationCenter defaultCenter]removeObserver:self]
,否則很有可能你會(huì)把系統(tǒng)的其他通知也移除了
普通NSObject:在iOS9之后行嗤,NSObject也會(huì)像ViewController一樣在dealloc時(shí)調(diào)用[[NSNotificationCenter defaultCenter]removeObserver:self]
方法已日,在iOS9之前的不會(huì)調(diào)用,需要自己寫(xiě)栅屏。
文字開(kāi)頭的那段代碼應(yīng)用場(chǎng)景:在使用類別的時(shí)候如果我們添加了通知飘千,那么我們是沒(méi)有辦法在類別里面重寫(xiě)dealloc的,如果不移除通知就會(huì)出現(xiàn)野指針栈雳,這個(gè)時(shí)候我們就可以在iOS9以上使用addObserver护奈,將通知的移除交給系統(tǒng),iOS9一下使用addObserverForName+weakSelf哥纫,雖然通知依然存在霉旗,但是不會(huì)調(diào)用doSomeThing方法(不要直接在block里面寫(xiě)處理過(guò)程啊)。
最后附上測(cè)試demo下載
再總結(jié)一下
當(dāng)你的通知沒(méi)辦法注銷的時(shí)候,iOS9以下addObserverForName可以不注銷通知的情況下(通知沒(méi)釋放)釋放監(jiān)聽(tīng)者鸵闪,iOS9以上使用addObserver可以不手動(dòng)注銷通知情況下釋放通知和監(jiān)聽(tīng)者檐晕,涉及到內(nèi)存使用問(wèn)題。關(guān)鍵在于沒(méi)辦法手動(dòng)注銷通知的情況下蚌讼,比如你的擴(kuò)展里面使用了通知棉姐,但是擴(kuò)展不支持調(diào)用dealloc.