-
通知的概念
一些基本的概念就不做介绍了,应该都明白,好了,直接上代码
为了方便查看,发送通知和接受通知就放在同一个文件里,一般项目不会这么用,但是操作都是一样的
可以po一下 [[NSNotificationCenter defaultCenter],你会发现其实就是个列表,包含了系统通知,用户通知等
- (void)viewDidLoad {
[super viewDidLoad];
//object:指定接受某个对象的通知,为nil表示可以接受任意对象的通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotifi:) name:@"EdisonNotif" object:nil];
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self sendNotification];
}
-(void)sendNotification{
NSLog(@"发送通知before:%@",[NSThread currentThread]);
[[NSNotificationCenter defaultCenter] postNotificationName:@"EdisonNotif" object:nil];
NSLog(@"发送通知after:%@",[NSThread currentThread]);
}
-(void)handleNotifi:(NSNotification*)notif{
NSLog(@"接收到通知了:%@",[NSThread currentThread]);
}
打印结果:
/*
2017-08-30 14:48:58.916 通知的底层解析[1422:94722] 发送通知before:<NSThread: 0x600000063500>{number = 1, name = main}
2017-08-30 14:48:58.916 通知的底层解析[1422:94722] 接收到通知了:<NSThread: 0x600000063500>{number = 1, name = main}
2017-08-30 14:48:58.917 通知的底层解析[1422:94722] 发送通知after:<NSThread: 0x600000063500>{number = 1, name = main}
*/
以上代码就是我们平时所写的发送注册通知了一般写法,点击屏幕发送一次通知,看清楚打印顺序,before->处理通知->after
这说明了是消息发完之后要等处理了消息才跑发送消息之后的代码,这跟多线程中的同步概念相似,所以在同步的通知的方法里不要做耗时的操作,要不然就阻塞主线程
-
使用(同步,异步)
上面介绍了同步,那么我们来说说异步的,就是我发送完之后就继续跑下面的代码,不需要去管接受通知的处理
比如异步,那么就要用到通知对列
//同步
-(void)sendNotification{
//每个线程都默认又一个通知队列,可以直接获取,也可以alloc
NSNotificationQueue * notificationQueue = [NSNotificationQueue defaultQueue];
NSNotification * notification = [NSNotification notificationWithName:@"EdisonNotif" object:nil];
//NSNotification * notificationTwo = [NSNotification notificationWithName:@"EdisonNotif" object:nil];
/*
(NSPostingStyle) 什么时候发送
NSPostWhenIdle = 1,//空闲时发送
NSPostASAP = 2,//尽快发送
NSPostNow = 3,//现在发送
<#(NSNotificationCoalescing)#>消息合并的方式
NSNotificationNoCoalescing = 0, //不合并
NSNotificationCoalescingOnName = 1,//按名称合并
NSNotificationCoalescingOnSender = 2,//按发送者合并
*/
NSLog(@"异步发送通知before:%@",[NSThread currentThread]);
[notificationQueue enqueueNotification:notification postingStyle:NSPostWhenIdle coalesceMask:NSNotificationCoalescingOnName forModes:nil];
//[notificationQueue enqueueNotification:notificationTwo postingStyle:NSPostNow coalesceMask:NSNotificationCoalescingOnName forModes:nil];
//[notificationQueue enqueueNotification:notification postingStyle:NSPostNow coalesceMask:NSNotificationCoalescingOnName forModes:nil];
NSLog(@"异步发送通知after:%@",[NSThread currentThread]);
/*
2017-08-30 15:43:13.068 通知的底层解析[4206:132048] 异步发送通知before:<NSThread: 0x60000007e180>{number = 1, name = main}
2017-08-30 15:43:13.068 通知的底层解析[4206:132048] 异步发送通知after:<NSThread: 0x60000007e180>{number = 1, name = main}
2017-08-30 15:43:13.069 通知的底层解析[4206:132048] 接收到通知了:<NSThread: 0x60000007e180>{number = 1, name = main}
*/
}
如果把发送通知的方法sendNotification改成如上,打印的结果就是表明是异步发送了
再看下通知队列跟线程的了关系,把点击屏幕直接发送通知,改成开启一个线程发送通知
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[NSThread detachNewThreadSelector:@selector(sendAsyncNotification) toTarget:self withObject:nil];
/*
2017-08-30 15:51:17.254 通知的底层解析[4244:137397] 异步发送通知before:<NSThread: 0x600000079480>{number = 3, name = (null)}
2017-08-30 15:51:17.255 通知的底层解析[4244:137397] 异步发送通知after:<NSThread: 0x600000079480>{number = 3, name = (null)}
*/
}
看打印结果,发现根本没有处理通知
再改下代码,用线程发送同步的通知中心
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//[self sendNotification];
[NSThread detachNewThreadSelector:@selector(sendNotification) toTarget:self withObject:nil];
//[self sendAsyncNotification];
}
/*
2017-08-30 15:54:49.932 通知的底层解析[4263:139861] 发送通知before:<NSThread: 0x60000026bc40>{number = 3, name = (null)}
2017-08-30 15:54:49.933 通知的底层解析[4263:139861] 接收到通知了:<NSThread: 0x60000026bc40>{number = 3, name = (null)}
2017-08-30 15:54:49.933 通知的底层解析[4263:139861] 发送通知after:<NSThread: 0x60000026bc40>{number = 3, name = (null)}
*/
而用线程发送同步的是可以接受到通知的,并且处理也是在线程里处理的
,这说通知队列跟线程是有关系的,再继续改代码,回到线程发送异步通知,只是把发送时机改成马上发送
[notificationQueue enqueueNotification:notification postingStyle:NSPostNow coalesceMask:NSNotificationNoCoalescing forModes:nil];
/*
2017-08-30 15:59:19.426 通知的底层解析[4276:143034] 异步发送通知before:<NSThread: 0x60800006a180>{number = 3, name = (null)}
2017-08-30 15:59:19.426 通知的底层解析[4276:143034] 接收到通知了:<NSThread: 0x60800006a180>{number = 3, name = (null)}
2017-08-30 15:59:19.426 通知的底层解析[4276:143034] 异步发送通知after:<NSThread: 0x60800006a180>{number = 3, name = (null)}
*/
这又能处理通知,所以可以说NSPostNow就是同步,其实呢,[[NSNotificationCenter defaultCenter]通知中心这句代码的意思就是:你在哪个线程里面就是获取当前线程的通知队列并且默认采用NSPostNow发送时机
NSLog(@"异步发送通知before:%@",[NSThread currentThread]);
[notificationQueue enqueueNotification:notification postingStyle:NSPostWhenIdle coalesceMask:NSNotificationNoCoalescing forModes:nil];
NSLog(@"异步发送通知after:%@",[NSThread currentThread]);
那么通知队列到底和线程有什么关系呢:每个线程都有一个通知队列,当线程结束了,通知队列就被释放了,所以当前选择发送时机为NSPostWhenIdle时也就是空闲的时候发送通知,通知队列就已经释放了,所以通知发送不出去了
如果线程不结束,就可以发送通知了,用runloop让线程不结束
//异步
-(void)sendAsyncNotification{
//每个线程都默认又一个通知队列,可以直接获取,也可以alloc
NSNotificationQueue * notificationQueue = [NSNotificationQueue defaultQueue];
NSNotification * notification = [NSNotification notificationWithName:@"EdisonNotif" object:nil];
NSLog(@"异步发送通知before:%@",[NSThread currentThread]);
[notificationQueue enqueueNotification:notification postingStyle:NSPostWhenIdle coalesceMask:NSNotificationNoCoalescing forModes:nil];
NSLog(@"异步发送通知after:%@",[NSThread currentThread]);
NSPort * port = [NSPort new];
[[NSRunLoop currentRunLoop] addPort:port forMode:NSRunLoopCommonModes];
[[NSRunLoop currentRunLoop] run];
}
/*
2017-08-30 16:12:55.098 通知的底层解析[4394:153794] 异步发送通知before:<NSThread: 0x600000070200>{number = 3, name = (null)}
2017-08-30 16:12:55.098 通知的底层解析[4394:153794] 异步发送通知after:<NSThread: 0x600000070200>{number = 3, name = (null)}
2017-08-30 16:12:55.099 通知的底层解析[4394:153794] 接收到通知了:<NSThread: 0x600000070200>{number = 3, name = (null)}
*/
这样通知就被发送出去了,而且发送和处理也在线程中,这还没有达到真正的异步是吧,应该发送在一个线程,处理在另一个线程
再来看下消息合并
//异步
-(void)sendAsyncNotification{
//每个线程都默认又一个通知队列,可以直接获取,也可以alloc
NSNotificationQueue * notificationQueue = [NSNotificationQueue defaultQueue];
NSNotification * notification = [NSNotification notificationWithName:@"EdisonNotif" object:nil];
NSNotification * notificationtwo = [NSNotification notificationWithName:@"EdisonNotif" object:nil];
/*
<#(NSNotificationCoalescing)#>消息合并的方式
NSNotificationNoCoalescing = 0, //不合并
NSNotificationCoalescingOnName = 1,//按名称合并
NSNotificationCoalescingOnSender = 2,//按发送者合并
*/
NSLog(@"异步发送通知before:%@",[NSThread currentThread]);
[notificationQueue enqueueNotification:notification postingStyle:NSPostWhenIdle coalesceMask:NSNotificationCoalescingOnName forModes:nil];
[notificationQueue enqueueNotification:notificationtwo postingStyle:NSPostWhenIdle coalesceMask:NSNotificationCoalescingOnName forModes:nil];
[notificationQueue enqueueNotification:notification postingStyle:NSPostWhenIdle coalesceMask:NSNotificationCoalescingOnName forModes:nil];
NSLog(@"异步发送通知after:%@",[NSThread currentThread]);
NSPort * port = [NSPort new];
[[NSRunLoop currentRunLoop] addPort:port forMode:NSRunLoopCommonModes];
[[NSRunLoop currentRunLoop] run];
}
/*
2017-08-30 16:22:16.149 通知的底层解析[4407:159094] 异步发送通知before:<NSThread: 0x608000265640>{number = 8, name = (null)}
2017-08-30 16:22:16.154 通知的底层解析[4407:159094] 异步发送通知after:<NSThread: 0x608000265640>{number = 8, name = (null)}
2017-08-30 16:22:16.154 通知的底层解析[4407:159094] 接收到通知了:<NSThread: 0x608000265640>{number = 8, name = (null)}
*/
设置成NSNotificationCoalescingOnName按名称合并,此时我连续发送三条,但是只处理了一次,
再继续,上面代码就只是把发送时机改成NSPostNow
[notificationQueue enqueueNotification:notification postingStyle:NSPostNow coalesceMask:NSNotificationCoalescingOnName forModes:nil];
[notificationQueue enqueueNotification:notificationTwo postingStyle:NSPostNow coalesceMask:NSNotificationCoalescingOnName forModes:nil];
[notificationQueue enqueueNotification:notification postingStyle:NSPostNow coalesceMask:NSNotificationCoalescingOnName forModes:nil];
/*
2017-08-30 16:30:04.114 通知的底层解析[4442:164620] 异步发送通知before:<NSThread: 0x600000269a40>{number = 3, name = (null)}
2017-08-30 16:30:04.115 通知的底层解析[4442:164620] 接收到通知了:<NSThread: 0x600000269a40>{number = 3, name = (null)}
2017-08-30 16:30:04.115 通知的底层解析[4442:164620] 接收到通知了:<NSThread: 0x600000269a40>{number = 3, name = (null)}
2017-08-30 16:30:04.115 通知的底层解析[4442:164620] 接收到通知了:<NSThread: 0x600000269a40>{number = 3, name = (null)}
2017-08-30 16:30:04.116 通知的底层解析[4442:164620] 异步发送通知after:<NSThread: 0x600000269a40>{number = 3, name = (null)}
*/
结果就打印了处理了三次通知,这个应该好理解吧,就跟dispatch_sync原理一样,就是得发送因为NSPostNow是同步的,所以发送第一条通知,得等处理完第一条通知,才跑发送第二条通知,这样肯定就没有合并消息一说了,因为这有点类似线程阻塞的意思,只有异步,就是三个发送通知全部跑完,在处理通知的时候看是否需要合并和怎么合并,再去处理
那么系统哪些消息是合并的
drawRect
-
通知队列(线程关系,消息合并等,系统哪些消息做了合并)
-
底层通信port(同线程处理,不同线程处理)
通知队列也可以实现异步,但是真正的异步还是得通过port
NSPort,底层所有的消息触发都是通过端口来进行操作的
- (void)viewDidLoad {
[super viewDidLoad];
//[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotifi:) name:@"EdisonNotif" object:nil];
_port =[[NSPort alloc] init];
//消息处理通过代理来处理的
_port.delegate=self;
//把端口加在哪个线程里,就在哪个线程进行处理,下面:加在当前线程的runloop里
[[NSRunLoop currentRunLoop] addPort:_port forMode:NSRunLoopCommonModes];
}
//发送消息
-(void)sendPort{
NSLog(@"port发送通知before:%@",[NSThread currentThread]);
[_port sendBeforeDate:[NSDate date] msgid:1212 components:nil from:nil reserved:0];
NSLog(@"port发送通知after:%@",[NSThread currentThread]);
}
//处理消息
- (void)handlePortMessage:(NSPortMessage *)message{
NSLog(@"port处理任务:%@",[NSThread currentThread]);
NSObject * messageObj = (NSObject*)message;
NSLog(@"=%@",[messageObj valueForKey:@"msgid"]);
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self sendPort];
}
/*
2017-08-30 17:09:28.715 通知的底层解析[8171:198651] port发送通知before:<NSThread: 0x600000075440>{number = 1, name = main}
2017-08-30 17:09:28.715 通知的底层解析[8171:198651] port发送通知after:<NSThread: 0x600000075440>{number = 1, name = main}
2017-08-30 17:09:28.715 通知的底层解析[8171:198651] port处理任务:<NSThread: 0x600000075440>{number = 1, name = main}
2017-08-30 17:09:28.715 通知的底层解析[8171:198651] =1212
*/
这样显然全部都在一个线程里,修改下代码
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[NSThread detachNewThreadSelector:@selector(sendPort) toTarget:self withObject:nil];
/*
*/
}
-
分析层次结构(通知中心)