最近看到一段有趣的代碼
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];
}
一開始看到就猜想難道不同的系統接受通知的姿勢不一樣?但是之前一直都在用addObserver
,也沒出現過問題啊,感覺這串代碼是多此一舉,就沒在管。后來有一次因為沒有removeObserver
引發了崩潰,才發現這串代碼是有深意的。最近整理了一下,留作筆記,因為這里面特別繞
規則只有一個,無論什么時候都不可以打破
規則:addObserverForName
的block
里面必須使用weakSelf
,否則會造成循環引用導致當前類無法釋放
注意事項
- 注意事項一:
doSomeThing
里面如果要刷新UI,最好是回調主線程。
因為通知的接收所在的線程
是基于發送通知所在的線程
,如果通知是在主線程發出的,通知的接收也是在主線程,如果通知的發送是在子線程,通知的接收也是在子線程。也就是說如果你不能保證你的通知一定是在主線程發送的,就最好回到主線程刷新UI
-(void)doSomeThing
{
//do something
dispatch_async(dispatch_get_main_queue(), ^{
// UI
});
}
- 注意事項二:通知的移除要調用各自的移除方法
addObserverForName
和addObserver
通知的移除方法是不一樣的
addObserver
通知的移除方法是[[NSNotificationCenter defaultCenter]removeObserver:self]
,這是最常見的,然而對于addObserverForName
是沒有用的,
addObserverForName
通知的移除方法是在聲明的時候要記錄通知變量
@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];
移除
打破概念特別注意事項
特別注意事項:通知的移除不一定非要寫
對于addObserverForName:
__weak typeof(self) weakSelf;
notificationObserver = [[NSNotificationCenter defaultCenter]addObserverForName:@"MYNotificationCenter" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
NSLog(@"addObserverForName接收到通知"); //如果沒有移除這個通知,每次接到通知時這個NSLog都會輸出
[weakSelf doSomeThing]; //如果沒有移除這個通知,如果是直接使用self,則這個通知所在NSObject或者ViewController都不會釋放,如果是使用weakSelf,則這個通知所在NSObject或者ViewController都會釋放,就不會再繼續調用doSomeThing方法
}];
對于addObserver:
這里要分ViewController和普通NSObject兩個說起
ViewController:在調用ViewController的dealloc的時候,系統會調用[[NSNotificationCenter defaultCenter]removeObserver:self]
方法,所以如果是在viewDidLoad中使用addObserver添加監聽者的話可以省掉移除。當然,如果是在viewWillAppear中添加的話,那么就要在viewWillAppear中自己移除,而且
,最好是使用[[NSNotificationCenter defaultCenter] removeObserver:self name:@"test" object:nil];
而非[[NSNotificationCenter defaultCenter]removeObserver:self]
,否則很有可能你會把系統的其他通知也移除了
普通NSObject:在iOS9之后,NSObject也會像ViewController一樣在dealloc時調用[[NSNotificationCenter defaultCenter]removeObserver:self]
方法,在iOS9之前的不會調用,需要自己寫。
文字開頭的那段代碼應用場景:在使用類別的時候如果我們添加了通知,那么我們是沒有辦法在類別里面重寫dealloc的,如果不移除通知就會出現野指針,這個時候我們就可以在iOS9以上使用addObserver,將通知的移除交給系統,iOS9一下使用addObserverForName+weakSelf,雖然通知依然存在,但是不會調用doSomeThing方法(不要直接在block里面寫處理過程啊)。
最后附上測試demo下載
再總結一下
當你的通知沒辦法注銷的時候,iOS9以下addObserverForName可以不注銷通知的情況下(通知沒釋放)釋放監聽者,iOS9以上使用addObserver可以不手動注銷通知情況下釋放通知和監聽者,涉及到內存使用問題。關鍵在于沒辦法手動注銷通知的情況下,比如你的擴展里面使用了通知,但是擴展不支持調用dealloc.