NSNotificationCenter

前幾天面試富途證券,被問到添加通知的相關問題,當時有幾個問題答錯了,在此總結。html

使用通知的要點

1.註冊多少次,他的執行代碼就會執行多少次

//一、註冊多個通知
for (int i =0; i<3; i++) {
     [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(notifycationMehod:) name:@"testNotifycation" object:nil];
 }
//二、傳遞通知
[[NSNotificationCenter defaultCenter]postNotificationName:@"testNotifycation" object:nil];

輸出結果面試



2016-12-01 17:06:23.868 NotifycationDemo[28703:10079806] receive notifycation object (null) 2016-12-01 17:06:23.868 NotifycationDemo[28703:10079806] receive notifycation object (null) 2016-12-01 17:06:23.869 NotifycationDemo[28703:10079806] receive notifycation object (null)

2.雖然註冊屢次通知,可是移除一次通知,同一個對象通知就會所有移除

 

3.addRemove相關方法成對出現咱們平時在使用通知的時候可能會在viewWillAppear方法裏面add觀察者,viewWillDisappear裏面remove,可是咱們若是忘記remove,那麼若是咱們導航到當前頁面再導航到另一個頁面,而後再退回當前頁面,這時候咱們會獲得2個輸出結果。

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    //註冊通知
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(notifycationMehod:) name:@"testNotifycation" object:nil];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    //沒有remove通知
}

2016-12-01 17:24:13.722 NotifycationDemo[28820:10087367] receive notifycation object (null)app

2016-12-01 17:24:13.723 NotifycationDemo[28820:10087367] receive notifycation object (null)async


4.移除一個name沒有add的通知,不會崩潰

[[NSNotificationCenter defaultCenter]removeObserver:self name:@"unknownName" object:nil];




5 . 一個對象添加了觀察者,可是沒有移除,程序不會崩潰,這是爲啥



@implementation NotifyTestClass

- (void)registerMyNotifycation{
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(notifycationMehod) name:@"testNotifycation" object:nil];
}

- (void)notifycationMehod{
    NSLog(@"NotifyTestClass receive notify");
}

- (void)dealloc{
    NSLog(@"NotifyTestClass dealloc");
}
@end

//局部變量 馬上釋放
 NotifyTestClass *nt = [[NotifyTestClass alloc]init];
 [nt registerMyNotifycation];
//發送通知
[[NSNotificationCenter defaultCenter]postNotificationName:@"testNotifycation" object:nil];

輸出結果post



蘋果官網文檔有說明,iOS 9.0以後NSNotificationCenter不會對一個dealloc的觀察者發送消息因此這樣就不會崩潰了。果然我換了一個8.1的手機測試,程序崩潰了,原來是作了優化。2016-12-01 19:29:15.039 NotifycationDemo[29035:10119206] NotifyTestClass dealloc

6 . ViewController對象在銷燬的時候,系統會對他監聽的通知進行清除

@implementation TwoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(notifycationMehod) name:@"testNotifycation" object:nil];
    // Do any additional setup after loading the view from its nib.
}
- (void)notifycationMehod{
    NSLog(@"TwoViewController receive notify");
}
- (void)dealloc{
    NSLog(@"TwoViewController dealloc");
}
@end

輸出結果測試



2016-12-02 16:36:42.175 NotifycationDemo[31359:10305477] TwoViewController dealloc 2016-12-02 16:36:42.176 NotifycationDemo[31359:10305477] removeObserver = <TwoViewController: 0x7fa42381d720>

7 . 處理方法是和post通知方法在同一線程同步執行的


所以咱們若是要接受一些通知更新UI的時候,咱們須要回到主線程來處理。
 dispatch_async(dispatch_get_main_queue(), ^{
        //do your UI
    });

 

Reference:

NSNotificationCenter/NSNotification 使用注意點全面解析優化

深刻思考NSNotificationspa

相關文章
相關標籤/搜索