// // ViewController.m // FKLTest // // Created by kun on 16/8/10. // Copyright © 2016年 kun. All rights reserved. // #import "ViewController.h" #import "FKLTool.h" #import "XMGDownloadTool.h" #import "PostNotificationViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; NSLog(@"******#receiveNotification--Begin#******"); [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveNotification) name:@"postNotification" object:nil]; NSLog(@"******#receiveNotification--End#******"); } - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { PostNotificationViewController *postVc = [[PostNotificationViewController alloc] init]; [self presentViewController:postVc animated:YES completion:nil]; } - (void)receiveNotification { NSLog(@"---begin---%s", __func__); sleep(5.0); NSLog(@"---after---%s", __func__); } @end
// // PostNotificationViewController.m // FKLTest // // Created by kun on 16/8/24. // Copyright © 2016年 kun. All rights reserved. // #import "PostNotificationViewController.h" @interface PostNotificationViewController () @end @implementation PostNotificationViewController - (void)viewDidLoad { [super viewDidLoad]; NSLog(@"******#postNotification--Begin#******"); [[NSNotificationCenter defaultCenter] postNotificationName:@"postNotification" object:nil]; NSLog(@"******#postNotification--End#******"); } @end
2016-08-24 18:54:07.713 FKLTest[1186:24510] ******#receiveNotification--Begin#****** 2016-08-24 18:54:07.714 FKLTest[1186:24510] ******#receiveNotification--End#****** 2016-08-24 18:56:15.805 FKLTest[1186:24510] ******#postNotification--Begin#****** 2016-08-24 18:56:15.806 FKLTest[1186:24510] ---begin----[ViewController receiveNotification] 2016-08-24 18:56:20.807 FKLTest[1186:24510] ---after----[ViewController receiveNotification] 2016-08-24 18:56:20.808 FKLTest[1186:24510] ******#postNotification--End#******
分析:监听通知必须要在发送通知之前设置好。发送通知,如果有监听此通知的观察者,会等待观察者的监听方法处理完后才会返回;如果没有则立马返回。