Notification 到底怎么玩 ?与线程是怎样的错综复杂关系?

 
区别并发队列串行队列主队列
同步(sync) 没有开启新线程,串行执行任务 没有开启新线程,串行执行任务 主线程调用:死锁卡住不执行
其他线程调用:没有开启新线程,串行执行任务
异步(async) 有开启新线程,并发执行任务 有开启新线程(1条),串行执行任务 没有开启新线程,串行执行任务

 


注册通知两种方式:
以下统称A、B两种方式 如何移除通知就不多说了,贴出参考链接:https://blog.csdn.net/st646889325/article/details/53300684
/* queue: 决定接收通知的线程 nil-与发通知的线程一致, currentQueue-与注册通知的线程一致, mainQueue-在主线程 usingBlock: 在规定的线程回调收到的通知 */ [[NSNotificationCenter defaultCenter] addObserverForName:kNotificationName1 object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) { [weakSelf receviedNotificaion1:note]; }]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receviedNotificaion2:) name:kNotificationName2 object:nil];

 

接收通知,二次处理 

- (void)receviedNotificaion1:(NSNotification *)note {
    
    NSLog(@"\n 收到 通知1 数据 ----- %@ ", [NSThread currentThread]);
    sleep(1.0);
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(pushData:)]) {
        [self.delegate pushData:@[@"11"]];
    }
}

- (void)receviedNotificaion2:(NSNotification *)note {
    
    NSLog(@"\n 收到 通知2 数据 ----- %@ ", [NSThread currentThread]);
    sleep(2.0);
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(pushData:)]) {
        [self.delegate pushData:@[@"22"]];
    }
}

 

 

发送通知不同方式:串行、并发、主队列三种方式

- (void)test:(UIButton *)button {
    
    dispatch_queue_t queue ;
    NSString *pushMethod = @"";
    if (button.tag == 100) {
       
        pushMethod = @"串行队列";
        queue = dispatch_queue_create("net.bujige.testQueue", DISPATCH_QUEUE_SERIAL);
    
    } else if (button.tag == 200) {
    
        pushMethod = @"并发队列";
        queue = dispatch_queue_create("net.bujige.testQueue", DISPATCH_QUEUE_CONCURRENT);
   
    } else if (button.tag == 300) {
       
        pushMethod = @"主队列";
        queue = dispatch_get_main_queue();
    }
    
    dispatch_async(queue, ^{
        // 追加任务1
        for (int i = 0; i < 2; ++i) {

            NSLog(@"\n %@ 发出 通知1 数据 ----- %@ ",pushMethod, [NSThread currentThread]);
            [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationName1
                                                                object:nil
                                                              userInfo:@{@"index":@(i+200)}];
            NSLog(@"\n %@ 结束 通知1  ----- %@ ", pushMethod, [NSThread currentThread]);
        }
    });
    
    dispatch_async(queue, ^{
        
        for (int i = 0; i < 2; i++) {
            
            NSLog(@"\n %@ 发出 通知2 数据 ----- %@ ",pushMethod, [NSThread currentThread]);
            [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationName2
                                                                object:nil
                                                              userInfo:@{@"index":@(i+100)}];
            NSLog(@"\n %@ 结束 通知2  ----- %@ ",pushMethod, [NSThread currentThread]);
        }
    });
}

 

主线程创建通知,串行队列 - 发通知(为啥在同一个线程 ?可以百度GCD, 参考:https://www.jianshu.com/p/2d57c72016c6)

注册A方式 queue->nil :发送、接收、代理回调、结束都在在同一个线程(线程3)

注册B方式:        发送、接收、代理回调、结束都在在同一个线程(线程3)

2018-11-18 14:49:22.513320+0800 NotificationQueuesDemo[57155:12888297] 
 注册 通知  ----- <NSThread: 0x600000062600>{number = 1, name = main} 
2018-11-18 14:49:25.744370+0800 NotificationQueuesDemo[57155:12888378] 
 串行队列 发出 通知1 数据 ----- <NSThread: 0x6000002615c0>{number = 3, name = (null)} 
2018-11-18 14:49:25.745143+0800 NotificationQueuesDemo[57155:12888378] 
 收到 通知1 数据 ----- <NSThread: 0x6000002615c0>{number = 3, name = (null)} 
2018-11-18 14:49:26.748952+0800 NotificationQueuesDemo[57155:12888378] 
 代理回调收到通知数据 (
    11
) ------ <NSThread: 0x6000002615c0>{number = 3, name = (null)} 

2018-11-18 14:49:26.749148+0800 NotificationQueuesDemo[57155:12888378] 
 串行队列 结束 通知1  ----- <NSThread: 0x6000002615c0>{number = 3, name = (null)} 
2018-11-18 14:49:26.749292+0800 NotificationQueuesDemo[57155:12888378] 
 串行队列 发出 通知1 数据 ----- <NSThread: 0x6000002615c0>{number = 3, name = (null)} 
2018-11-18 14:49:26.749438+0800 NotificationQueuesDemo[57155:12888378] 
 收到 通知1 数据 ----- <NSThread: 0x6000002615c0>{number = 3, name = (null)} 
2018-11-18 14:49:27.752300+0800 NotificationQueuesDemo[57155:12888378] 
 代理回调收到通知数据 (
    11
) ------ <NSThread: 0x6000002615c0>{number = 3, name = (null)} 

2018-11-18 14:49:27.755329+0800 NotificationQueuesDemo[57155:12888378] 
 串行队列 结束 通知1  ----- <NSThread: 0x6000002615c0>{number = 3, name = (null)} 
2018-11-18 14:49:27.757479+0800 NotificationQueuesDemo[57155:12888378] 
 串行队列 发出 通知2 数据 ----- <NSThread: 0x6000002615c0>{number = 3, name = (null)} 
2018-11-18 14:49:27.758203+0800 NotificationQueuesDemo[57155:12888378] 
 收到 通知2 数据 ----- <NSThread: 0x6000002615c0>{number = 3, name = (null)} 
2018-11-18 14:49:29.759594+0800 NotificationQueuesDemo[57155:12888378] 
 代理回调收到通知数据 (
    22
) ------ <NSThread: 0x6000002615c0>{number = 3, name = (null)} 

2018-11-18 14:49:29.760352+0800 NotificationQueuesDemo[57155:12888378] 
 串行队列 结束 通知2  ----- <NSThread: 0x6000002615c0>{number = 3, name = (null)} 
2018-11-18 14:49:29.760575+0800 NotificationQueuesDemo[57155:12888378] 
 串行队列 发出 通知2 数据 ----- <NSThread: 0x6000002615c0>{number = 3, name = (null)} 
2018-11-18 14:49:29.760703+0800 NotificationQueuesDemo[57155:12888378] 
 收到 通知2 数据 ----- <NSThread: 0x6000002615c0>{number = 3, name = (null)} 
2018-11-18 14:49:31.763164+0800 NotificationQueuesDemo[57155:12888378] 
 代理回调收到通知数据 (
    22
) ------ <NSThread: 0x6000002615c0>{number = 3, name = (null)} 

2018-11-18 14:49:31.763490+0800 NotificationQueuesDemo[57155:12888378] 
 串行队列 结束 通知2  ----- <NSThread: 0x6000002615c0>{number = 3, name = (null)} 

 

主线程创建通知,并发队列 - 发通知 (为啥不在同一个线程 ?可以百度GCD, 参考:https://www.jianshu.com/p/2d57c72016c6)

注册A方式 queue->nil :发送、接收、代理回调、结束都在在同一个线程(线程4)

注册B方式:        发送、接收、代理回调、结束都在在同一个线程(线程5)

2018-11-18 14:52:16.545835+0800 NotificationQueuesDemo[57155:12889770] 
 并发队列 发出 通知1 数据 ----- <NSThread: 0x600000261ec0>{number = 4, name = (null)} 
2018-11-18 14:52:16.545954+0800 NotificationQueuesDemo[57155:12888377] 
 并发队列 发出 通知2 数据 ----- <NSThread: 0x600000262a00>{number = 5, name = (null)} 
2018-11-18 14:52:16.547021+0800 NotificationQueuesDemo[57155:12889770] 
 收到 通知1 数据 ----- <NSThread: 0x600000261ec0>{number = 4, name = (null)} 
2018-11-18 14:52:16.547588+0800 NotificationQueuesDemo[57155:12888377] 
 收到 通知2 数据 ----- <NSThread: 0x600000262a00>{number = 5, name = (null)} 
2018-11-18 14:52:17.548786+0800 NotificationQueuesDemo[57155:12889770] 
 代理回调收到通知数据 (
    11
) ------ <NSThread: 0x600000261ec0>{number = 4, name = (null)} 

2018-11-18 14:52:17.549125+0800 NotificationQueuesDemo[57155:12889770] 
 并发队列 结束 通知1  ----- <NSThread: 0x600000261ec0>{number = 4, name = (null)} 
2018-11-18 14:52:17.549425+0800 NotificationQueuesDemo[57155:12889770] 
 并发队列 发出 通知1 数据 ----- <NSThread: 0x600000261ec0>{number = 4, name = (null)} 
2018-11-18 14:52:17.550082+0800 NotificationQueuesDemo[57155:12889770] 
 收到 通知1 数据 ----- <NSThread: 0x600000261ec0>{number = 4, name = (null)} 
2018-11-18 14:52:18.552212+0800 NotificationQueuesDemo[57155:12888377] 
 代理回调收到通知数据 (
    22
) ------ <NSThread: 0x600000262a00>{number = 5, name = (null)} 

2018-11-18 14:52:18.552238+0800 NotificationQueuesDemo[57155:12889770] 
 代理回调收到通知数据 (
    11
) ------ <NSThread: 0x600000261ec0>{number = 4, name = (null)} 

2018-11-18 14:52:18.552535+0800 NotificationQueuesDemo[57155:12888377] 
 并发队列 结束 通知2  ----- <NSThread: 0x600000262a00>{number = 5, name = (null)} 
2018-11-18 14:52:18.552591+0800 NotificationQueuesDemo[57155:12889770] 
 并发队列 结束 通知1  ----- <NSThread: 0x600000261ec0>{number = 4, name = (null)} 
2018-11-18 14:52:18.552824+0800 NotificationQueuesDemo[57155:12888377] 
 并发队列 发出 通知2 数据 ----- <NSThread: 0x600000262a00>{number = 5, name = (null)} 
2018-11-18 14:52:18.553060+0800 NotificationQueuesDemo[57155:12888377] 
 收到 通知2 数据 ----- <NSThread: 0x600000262a00>{number = 5, name = (null)} 
2018-11-18 14:52:20.556747+0800 NotificationQueuesDemo[57155:12888377] 
 代理回调收到通知数据 (
    22
) ------ <NSThread: 0x600000262a00>{number = 5, name = (null)} 

2018-11-18 14:52:20.557005+0800 NotificationQueuesDemo[57155:12888377] 
 并发队列 结束 通知2  ----- <NSThread: 0x600000262a00>{number = 5, name = (null)} 

  

主线程创建通知,主队列 - 发通知 

注册A方式 queue->nil :发送、接收、代理回调、结束都在在同一个线程(main)

注册B方式:        发送、接收、代理回调、结束都在在同一个线程(main)

2018-11-18 14:54:56.908301+0800 NotificationQueuesDemo[57155:12888297] 
 主队列 发出 通知1 数据 ----- <NSThread: 0x600000062600>{number = 1, name = main} 
2018-11-18 14:54:56.908828+0800 NotificationQueuesDemo[57155:12888297] 
 收到 通知1 数据 ----- <NSThread: 0x600000062600>{number = 1, name = main} 
2018-11-18 14:54:57.909397+0800 NotificationQueuesDemo[57155:12888297] 
 代理回调收到通知数据 (
    11
) ------ <NSThread: 0x600000062600>{number = 1, name = main} 

2018-11-18 14:54:57.909782+0800 NotificationQueuesDemo[57155:12888297] 
 主队列 结束 通知1  ----- <NSThread: 0x600000062600>{number = 1, name = main} 
2018-11-18 14:54:57.910096+0800 NotificationQueuesDemo[57155:12888297] 
 主队列 发出 通知1 数据 ----- <NSThread: 0x600000062600>{number = 1, name = main} 
2018-11-18 14:54:57.910454+0800 NotificationQueuesDemo[57155:12888297] 
 收到 通知1 数据 ----- <NSThread: 0x600000062600>{number = 1, name = main} 
2018-11-18 14:54:58.911167+0800 NotificationQueuesDemo[57155:12888297] 
 代理回调收到通知数据 (
    11
) ------ <NSThread: 0x600000062600>{number = 1, name = main} 

2018-11-18 14:54:58.911559+0800 NotificationQueuesDemo[57155:12888297] 
 主队列 结束 通知1  ----- <NSThread: 0x600000062600>{number = 1, name = main} 
2018-11-18 14:54:58.911814+0800 NotificationQueuesDemo[57155:12888297] 
 主队列 发出 通知2 数据 ----- <NSThread: 0x600000062600>{number = 1, name = main} 
2018-11-18 14:54:58.912215+0800 NotificationQueuesDemo[57155:12888297] 
 收到 通知2 数据 ----- <NSThread: 0x600000062600>{number = 1, name = main} 
2018-11-18 14:55:00.913580+0800 NotificationQueuesDemo[57155:12888297] 
 代理回调收到通知数据 (
    22
) ------ <NSThread: 0x600000062600>{number = 1, name = main} 

2018-11-18 14:55:00.913829+0800 NotificationQueuesDemo[57155:12888297] 
 主队列 结束 通知2  ----- <NSThread: 0x600000062600>{number = 1, name = main} 
2018-11-18 14:55:00.913981+0800 NotificationQueuesDemo[57155:12888297] 
 主队列 发出 通知2 数据 ----- <NSThread: 0x600000062600>{number = 1, name = main} 
2018-11-18 14:55:00.914128+0800 NotificationQueuesDemo[57155:12888297] 
 收到 通知2 数据 ----- <NSThread: 0x600000062600>{number = 1, name = main} 
2018-11-18 14:55:02.915882+0800 NotificationQueuesDemo[57155:12888297] 
 代理回调收到通知数据 (
    22
) ------ <NSThread: 0x600000062600>{number = 1, name = main} 

2018-11-18 14:55:02.916972+0800 NotificationQueuesDemo[57155:12888297] 
 主队列 结束 通知2  ----- <NSThread: 0x600000062600>{number = 1, name = main} 

  

主线程创建通知,串行、并发队列 - 发通知 (如果主队列发则整个流程都在主队列)

注册A方式 queue->[NSOpetationQueue currentQueue] (或[NSOpetationQueue mainQueue] )

发送(子线程3)、接收(主线程main)、代理回调(主线程main)、结束(子线程3)

2018-11-18 14:57:19.560247+0800 NotificationQueuesDemo[57228:12892840] 
 注册 通知  <NSThread: 0x600000071600>{number = 1, name = main}----- <NSOperationQueue: 0x600000225fc0>{name = 'NSOperationQueue Main Queue'} 
2018-11-18 14:57:35.673019+0800 NotificationQueuesDemo[57228:12893025] 
 串行队列 发出 通知1 数据 ----- <NSThread: 0x600000279bc0>{number = 3, name = (null)} 
2018-11-18 14:57:35.677783+0800 NotificationQueuesDemo[57228:12892840] 
 收到 通知1 数据 ----- <NSThread: 0x600000071600>{number = 1, name = main} 
2018-11-18 14:57:36.679568+0800 NotificationQueuesDemo[57228:12892840] 
 代理回调收到通知数据 (
    11
) ------ <NSThread: 0x600000071600>{number = 1, name = main} 

2018-11-18 14:57:36.680195+0800 NotificationQueuesDemo[57228:12893025] 
 串行队列 结束 通知1  ----- <NSThread: 0x600000279bc0>{number = 3, name = (null)} 
2018-11-18 14:57:36.681018+0800 NotificationQueuesDemo[57228:12893025] 
 串行队列 发出 通知1 数据 ----- <NSThread: 0x600000279bc0>{number = 3, name = (null)} 
2018-11-18 14:57:36.681831+0800 NotificationQueuesDemo[57228:12892840] 
 收到 通知1 数据 ----- <NSThread: 0x600000071600>{number = 1, name = main} 
2018-11-18 14:57:37.683226+0800 NotificationQueuesDemo[57228:12892840] 
 代理回调收到通知数据 (
    11
) ------ <NSThread: 0x600000071600>{number = 1, name = main} 

2018-11-18 14:57:37.684008+0800 NotificationQueuesDemo[57228:12893025] 
 串行队列 结束 通知1  ----- <NSThread: 0x600000279bc0>{number = 3, name = (null)} 
2018-11-18 14:57:37.684236+0800 NotificationQueuesDemo[57228:12893025] 
 串行队列 发出 通知2 数据 ----- <NSThread: 0x600000279bc0>{number = 3, name = (null)} 
2018-11-18 14:57:37.684413+0800 NotificationQueuesDemo[57228:12893025] 
 收到 通知2 数据 ----- <NSThread: 0x600000279bc0>{number = 3, name = (null)} 
2018-11-18 14:57:39.685880+0800 NotificationQueuesDemo[57228:12893025] 
 代理回调收到通知数据 (
    22
) ------ <NSThread: 0x600000279bc0>{number = 3, name = (null)} 

2018-11-18 14:57:39.686221+0800 NotificationQueuesDemo[57228:12893025] 
 串行队列 结束 通知2  ----- <NSThread: 0x600000279bc0>{number = 3, name = (null)} 
2018-11-18 14:57:39.686460+0800 NotificationQueuesDemo[57228:12893025] 
 串行队列 发出 通知2 数据 ----- <NSThread: 0x600000279bc0>{number = 3, name = (null)} 
2018-11-18 14:57:39.686704+0800 NotificationQueuesDemo[57228:12893025] 
 收到 通知2 数据 ----- <NSThread: 0x600000279bc0>{number = 3, name = (null)} 
2018-11-18 14:57:41.688942+0800 NotificationQueuesDemo[57228:12893025] 
 代理回调收到通知数据 (
    22
) ------ <NSThread: 0x600000279bc0>{number = 3, name = (null)} 

2018-11-18 14:57:41.689344+0800 NotificationQueuesDemo[57228:12893025] 
 串行队列 结束 通知2  ----- <NSThread: 0x600000279bc0>{number = 3, name = (null)} 

  

继续!!!

 

 

子线程注册通知,串行队列 - 发通知

注册A方式 queue->nil :发送、接收、代理回调、结束都在在同一个线程(线程3)

注册B方式:        发送、接收、代理回调、结束都在在同一个线程(线程3)

2018-11-18 15:13:15.225519+0800 NotificationQueuesDemo[57295:12900324] 
 注册 通知  <NSThread: 0x60000026a700>{number = 3, name = (null)}----- (null) 
2018-11-18 15:13:19.809615+0800 NotificationQueuesDemo[57295:12900324] 
 串行队列 发出 通知1 数据 ----- <NSThread: 0x60000026a700>{number = 3, name = (null)} 
2018-11-18 15:13:19.810567+0800 NotificationQueuesDemo[57295:12900324] 
 收到 通知1 数据 ----- <NSThread: 0x60000026a700>{number = 3, name = (null)} 
2018-11-18 15:13:20.813167+0800 NotificationQueuesDemo[57295:12900324] 
 代理回调收到通知数据 (
    11
) ------ <NSThread: 0x60000026a700>{number = 3, name = (null)} 

2018-11-18 15:13:20.813373+0800 NotificationQueuesDemo[57295:12900324] 
 串行队列 结束 通知1  ----- <NSThread: 0x60000026a700>{number = 3, name = (null)} 
2018-11-18 15:13:20.814090+0800 NotificationQueuesDemo[57295:12900324] 
 串行队列 发出 通知1 数据 ----- <NSThread: 0x60000026a700>{number = 3, name = (null)} 
2018-11-18 15:13:20.814890+0800 NotificationQueuesDemo[57295:12900324] 
 收到 通知1 数据 ----- <NSThread: 0x60000026a700>{number = 3, name = (null)} 
2018-11-18 15:13:21.818488+0800 NotificationQueuesDemo[57295:12900324] 
 代理回调收到通知数据 (
    11
) ------ <NSThread: 0x60000026a700>{number = 3, name = (null)} 

2018-11-18 15:13:21.818717+0800 NotificationQueuesDemo[57295:12900324] 
 串行队列 结束 通知1  ----- <NSThread: 0x60000026a700>{number = 3, name = (null)} 
2018-11-18 15:13:21.819213+0800 NotificationQueuesDemo[57295:12900324] 
 串行队列 发出 通知2 数据 ----- <NSThread: 0x60000026a700>{number = 3, name = (null)} 
2018-11-18 15:13:21.819659+0800 NotificationQueuesDemo[57295:12900324] 
 收到 通知2 数据 ----- <NSThread: 0x60000026a700>{number = 3, name = (null)} 
2018-11-18 15:13:23.820382+0800 NotificationQueuesDemo[57295:12900324] 
 代理回调收到通知数据 (
    22
) ------ <NSThread: 0x60000026a700>{number = 3, name = (null)} 

2018-11-18 15:13:23.820766+0800 NotificationQueuesDemo[57295:12900324] 
 串行队列 结束 通知2  ----- <NSThread: 0x60000026a700>{number = 3, name = (null)} 
2018-11-18 15:13:23.821107+0800 NotificationQueuesDemo[57295:12900324] 
 串行队列 发出 通知2 数据 ----- <NSThread: 0x60000026a700>{number = 3, name = (null)} 
2018-11-18 15:13:23.821451+0800 NotificationQueuesDemo[57295:12900324] 
 收到 通知2 数据 ----- <NSThread: 0x60000026a700>{number = 3, name = (null)} 
2018-11-18 15:13:25.822801+0800 NotificationQueuesDemo[57295:12900324] 
 代理回调收到通知数据 (
    22
) ------ <NSThread: 0x60000026a700>{number = 3, name = (null)} 

2018-11-18 15:13:25.823148+0800 NotificationQueuesDemo[57295:12900324] 
 串行队列 结束 通知2  ----- <NSThread: 0x60000026a700>{number = 3, name = (null)} 

  

子线程注册通知,并发队列 - 发通知

注册A方式 queue->nil :发送、接收、代理回调、结束都在在同一个线程(线程4)

注册B方式:        发送、接收、代理回调、结束都在在同一个线程(线程5)

2018-11-18 15:15:34.948496+0800 NotificationQueuesDemo[57295:12901429] 
 并发队列 发出 通知1 数据 ----- <NSThread: 0x60400027a940>{number = 4, name = (null)} 
2018-11-18 15:15:34.948565+0800 NotificationQueuesDemo[57295:12900332] 
 并发队列 发出 通知2 数据 ----- <NSThread: 0x60400027a980>{number = 5, name = (null)} 
2018-11-18 15:15:34.949852+0800 NotificationQueuesDemo[57295:12901429] 
 收到 通知1 数据 ----- <NSThread: 0x60400027a940>{number = 4, name = (null)} 
2018-11-18 15:15:34.950626+0800 NotificationQueuesDemo[57295:12900332] 
 收到 通知2 数据 ----- <NSThread: 0x60400027a980>{number = 5, name = (null)} 
2018-11-18 15:15:35.955805+0800 NotificationQueuesDemo[57295:12901429] 
 代理回调收到通知数据 (
    11
) ------ <NSThread: 0x60400027a940>{number = 4, name = (null)} 

2018-11-18 15:15:35.956233+0800 NotificationQueuesDemo[57295:12901429] 
 并发队列 结束 通知1  ----- <NSThread: 0x60400027a940>{number = 4, name = (null)} 
2018-11-18 15:15:35.956565+0800 NotificationQueuesDemo[57295:12901429] 
 并发队列 发出 通知1 数据 ----- <NSThread: 0x60400027a940>{number = 4, name = (null)} 
2018-11-18 15:15:35.957801+0800 NotificationQueuesDemo[57295:12901429] 
 收到 通知1 数据 ----- <NSThread: 0x60400027a940>{number = 4, name = (null)} 
2018-11-18 15:15:36.952033+0800 NotificationQueuesDemo[57295:12900332] 
 代理回调收到通知数据 (
    22
) ------ <NSThread: 0x60400027a980>{number = 5, name = (null)} 

2018-11-18 15:15:36.952498+0800 NotificationQueuesDemo[57295:12900332] 
 并发队列 结束 通知2  ----- <NSThread: 0x60400027a980>{number = 5, name = (null)} 
2018-11-18 15:15:36.953106+0800 NotificationQueuesDemo[57295:12900332] 
 并发队列 发出 通知2 数据 ----- <NSThread: 0x60400027a980>{number = 5, name = (null)} 
2018-11-18 15:15:36.953436+0800 NotificationQueuesDemo[57295:12900332] 
 收到 通知2 数据 ----- <NSThread: 0x60400027a980>{number = 5, name = (null)} 
2018-11-18 15:15:36.958766+0800 NotificationQueuesDemo[57295:12901429] 
 代理回调收到通知数据 (
    11
) ------ <NSThread: 0x60400027a940>{number = 4, name = (null)} 

2018-11-18 15:15:36.958991+0800 NotificationQueuesDemo[57295:12901429] 
 并发队列 结束 通知1  ----- <NSThread: 0x60400027a940>{number = 4, name = (null)} 
2018-11-18 15:15:38.958522+0800 NotificationQueuesDemo[57295:12900332] 
 代理回调收到通知数据 (
    22
) ------ <NSThread: 0x60400027a980>{number = 5, name = (null)} 

2018-11-18 15:15:38.958864+0800 NotificationQueuesDemo[57295:12900332] 
 并发队列 结束 通知2  ----- <NSThread: 0x60400027a980>{number = 5, name = (null)} 

  

子线程注册通知,主队列 - 发通知

注册A方式 queue->nil :发送、接收、代理回调、结束都在在同一个线程(main)

注册B方式:        发送、接收、代理回调、结束都在在同一个线程(main)

2018-11-18 15:17:10.080285+0800 NotificationQueuesDemo[57295:12900234] 
 主队列 发出 通知1 数据 ----- <NSThread: 0x60000006d7c0>{number = 1, name = main} 
2018-11-18 15:17:10.080699+0800 NotificationQueuesDemo[57295:12900234] 
 收到 通知1 数据 ----- <NSThread: 0x60000006d7c0>{number = 1, name = main} 
2018-11-18 15:17:11.082156+0800 NotificationQueuesDemo[57295:12900234] 
 代理回调收到通知数据 (
    11
) ------ <NSThread: 0x60000006d7c0>{number = 1, name = main} 

2018-11-18 15:17:11.082575+0800 NotificationQueuesDemo[57295:12900234] 
 主队列 结束 通知1  ----- <NSThread: 0x60000006d7c0>{number = 1, name = main} 
2018-11-18 15:17:11.082917+0800 NotificationQueuesDemo[57295:12900234] 
 主队列 发出 通知1 数据 ----- <NSThread: 0x60000006d7c0>{number = 1, name = main} 
2018-11-18 15:17:11.083250+0800 NotificationQueuesDemo[57295:12900234] 
 收到 通知1 数据 ----- <NSThread: 0x60000006d7c0>{number = 1, name = main} 
2018-11-18 15:17:12.083721+0800 NotificationQueuesDemo[57295:12900234] 
 代理回调收到通知数据 (
    11
) ------ <NSThread: 0x60000006d7c0>{number = 1, name = main} 

2018-11-18 15:17:12.083979+0800 NotificationQueuesDemo[57295:12900234] 
 主队列 结束 通知1  ----- <NSThread: 0x60000006d7c0>{number = 1, name = main} 
2018-11-18 15:17:12.084139+0800 NotificationQueuesDemo[57295:12900234] 
 主队列 发出 通知2 数据 ----- <NSThread: 0x60000006d7c0>{number = 1, name = main} 
2018-11-18 15:17:12.084274+0800 NotificationQueuesDemo[57295:12900234] 
 收到 通知2 数据 ----- <NSThread: 0x60000006d7c0>{number = 1, name = main} 
2018-11-18 15:17:14.085683+0800 NotificationQueuesDemo[57295:12900234] 
 代理回调收到通知数据 (
    22
) ------ <NSThread: 0x60000006d7c0>{number = 1, name = main} 

2018-11-18 15:17:14.086092+0800 NotificationQueuesDemo[57295:12900234] 
 主队列 结束 通知2  ----- <NSThread: 0x60000006d7c0>{number = 1, name = main} 
2018-11-18 15:17:14.086430+0800 NotificationQueuesDemo[57295:12900234] 
 主队列 发出 通知2 数据 ----- <NSThread: 0x60000006d7c0>{number = 1, name = main} 
2018-11-18 15:17:14.086784+0800 NotificationQueuesDemo[57295:12900234] 
 收到 通知2 数据 ----- <NSThread: 0x60000006d7c0>{number = 1, name = main} 
2018-11-18 15:17:16.088062+0800 NotificationQueuesDemo[57295:12900234] 
 代理回调收到通知数据 (
    22
) ------ <NSThread: 0x60000006d7c0>{number = 1, name = main} 

2018-11-18 15:17:16.089131+0800 NotificationQueuesDemo[57295:12900234] 
 主队列 结束 通知2  ----- <NSThread: 0x60000006d7c0>{number = 1, name = main} 

  

子线程注册通知,串行、并发队列 - 发通知  ,注册A方式 queue->[NSOpetationQueue currentQueue] 

串行,发送、接收、代理回调、结束 同一个子线程

并行 ,发送、接收、代理回调、结束 分别两个子线程

主队列,发送、接收、代理回调、结束  都在主线程

 

子线程注册通知,串行、并发队列 - 发通知  ,注册A方式 queue->[NSOpetationQueue mainQueue] 

串行和并行,发送(子线程3)、接收(主线程main)、代理回调(主线程main)、结束(子线程3)

主队列 ,  整个流程都在main

 

无论在 主线程、子线程 注册通知通过以上测试,可以大概得出这样的论断:

发通知->收到通知->通知处理代码->整个通知结束,这是个串行队列,如果通知处理过程复杂,那么通知结束的log就越晚打印,就是说后续代码越晚执行,但是不在主队列玩是不会造成 主线程阻塞的(不会卡主UI)。

 

 那么问题来了,我只是发个通知而已,为啥要等接收通知的人干完他的事,然后我才能继续赶我自己的事儿呢 ?

我只管发通知,谁愿意要谁要,但是不能阻碍我继续干活,谁收到通知以后自己找个房间自己玩(自己开个线程玩儿去),不要耽误我继续。

 

所以,自己开个子线程处理收到通知以后的事情。

         串行、并行都解决问题,要看具体场景。
         
        queue1 = dispatch_queue_create("com.notification.queue1", DISPATCH_QUEUE_SERIAL);
        queue2 = dispatch_queue_create("com.notification.queue2", DISPATCH_QUEUE_SERIAL);
        
        // queue1 = dispatch_queue_create("com.notification.queue1", DISPATCH_QUEUE_CONCURRENT);
        // queue2 = dispatch_queue_create("com.notification.queue2", DISPATCH_QUEUE_CONCURRENT);  

- (void)receviedNotificaion1:(NSNotification *)note {
    
    NSLog(@"\n 收到 通知1 数据 ----- %@ ", [NSThread currentThread]);
//    sleep(1.0);
    
    dispatch_async(queue1, ^{
        
        [self.dataSource1 addObject:note];
        NSLog(@"\n 通知1 加入数组 %lu ----- %@ ",(unsigned long)self.dataSource1.count, [NSThread currentThread]);

    });
    
    dispatch_async(queue1, ^{
        
        if (self.dataSource1.count == 20) {
            
            if (self.delegate && [self.delegate respondsToSelector:@selector(pushData:type:)]) {
                [self.delegate pushData:self.dataSource1 type:1];
            }
            
            [self.dataSource1 removeAllObjects];
        }
    });
}

- (void)receviedNotificaion2:(NSNotification *)note {
    
    NSLog(@"\n 收到 通知2 数据 ----- %@ ", [NSThread currentThread]);
//    sleep(2.0);

    dispatch_async(queue2, ^{
        
        [self.dataSource2 addObject:note];
        NSLog(@"\n 通知2加入数组 %lu ----- %@ ",(unsigned long)self.dataSource2.count, [NSThread currentThread]);
        
    });
    
    dispatch_async(queue2, ^{
        
        if (self.dataSource2.count == 20) {
            
            if (self.delegate && [self.delegate respondsToSelector:@selector(pushData:type:)]) {
                [self.delegate pushData:self.dataSource2 type:2];
            }
            
            [self.dataSource2 removeAllObjects];
        }
    });
}

  

看一下log就知道了 发出->收到通知->通知结束 在同一个线程4, 收到通知后数据处理在另一个线程5.

2018-11-18 16:14:38.628166+0800 NotificationQueuesDemo[57810:12931075] 
 注册 通知  <NSThread: 0x60000027d640>{number = 3, name = (null)}----- (null) 
2018-11-18 16:14:45.060534+0800 NotificationQueuesDemo[57810:12931074] 
 串行队列 发出 通知1 数据 ----- <NSThread: 0x6000004625c0>{number = 4, name = (null)} 
2018-11-18 16:14:45.061409+0800 NotificationQueuesDemo[57810:12931074] 
 收到 通知1 数据 ----- <NSThread: 0x6000004625c0>{number = 4, name = (null)} 
2018-11-18 16:14:45.062306+0800 NotificationQueuesDemo[57810:12931074] 
 串行队列 结束 通知1  ----- <NSThread: 0x6000004625c0>{number = 4, name = (null)} 
2018-11-18 16:14:45.062306+0800 NotificationQueuesDemo[57810:12931075] 
 通知1 加入数组 1 ----- <NSThread: 0x60000027d640>{number = 3, name = (null)} 
2018-11-18 16:14:45.062955+0800 NotificationQueuesDemo[57810:12931074] 
 串行队列 发出 通知1 数据 ----- <NSThread: 0x6000004625c0>{number = 4, name = (null)} 
2018-11-18 16:14:45.063466+0800 NotificationQueuesDemo[57810:12931074] 
 收到 通知1 数据 ----- <NSThread: 0x6000004625c0>{number = 4, name = (null)} 
2018-11-18 16:14:45.063882+0800 NotificationQueuesDemo[57810:12931074] 
 串行队列 结束 通知1  ----- <NSThread: 0x6000004625c0>{number = 4, name = (null)} 
2018-11-18 16:14:45.063888+0800 NotificationQueuesDemo[57810:12931075] 
 通知1 加入数组 2 ----- <NSThread: 0x60000027d640>{number = 3, name = (null)} 
2018-11-18 16:14:45.063962+0800 NotificationQueuesDemo[57810:12931076] 
 代理回调收到 通知1 数据 2个   ------ <NSThread: 0x60400046e640>{number = 5, name = (null)} 

2018-11-18 16:14:45.064262+0800 NotificationQueuesDemo[57810:12931074] 
 串行队列 发出 通知1 数据 ----- <NSThread: 0x6000004625c0>{number = 4, name = (null)} 
2018-11-18 16:14:45.065350+0800 NotificationQueuesDemo[57810:12931074] 
 收到 通知1 数据 ----- <NSThread: 0x6000004625c0>{number = 4, name = (null)} 
2018-11-18 16:14:45.065780+0800 NotificationQueuesDemo[57810:12931074] 
 串行队列 结束 通知1  ----- <NSThread: 0x6000004625c0>{number = 4, name = (null)} 
2018-11-18 16:14:45.065780+0800 NotificationQueuesDemo[57810:12931076] 
 通知1 加入数组 1 ----- <NSThread: 0x60400046e640>{number = 5, name = (null)} 
2018-11-18 16:14:45.066347+0800 NotificationQueuesDemo[57810:12931074] 
 串行队列 发出 通知2 数据 ----- <NSThread: 0x6000004625c0>{number = 4, name = (null)} 
2018-11-18 16:14:45.066767+0800 NotificationQueuesDemo[57810:12931074] 
 收到 通知2 数据 ----- <NSThread: 0x6000004625c0>{number = 4, name = (null)} 
2018-11-18 16:14:45.067470+0800 NotificationQueuesDemo[57810:12931074] 
 串行队列 结束 通知2  ----- <NSThread: 0x6000004625c0>{number = 4, name = (null)} 
2018-11-18 16:14:45.067474+0800 NotificationQueuesDemo[57810:12931076] 
 通知2加入数组 0 ----- <NSThread: 0x60400046e640>{number = 5, name = (null)} 
2018-11-18 16:14:45.068284+0800 NotificationQueuesDemo[57810:12931074] 
 串行队列 发出 通知2 数据 ----- <NSThread: 0x6000004625c0>{number = 4, name = (null)} 
2018-11-18 16:14:45.068453+0800 NotificationQueuesDemo[57810:12931074] 
 收到 通知2 数据 ----- <NSThread: 0x6000004625c0>{number = 4, name = (null)} 
2018-11-18 16:14:45.068806+0800 NotificationQueuesDemo[57810:12931074] 
 串行队列 结束 通知2  ----- <NSThread: 0x6000004625c0>{number = 4, name = (null)} 
2018-11-18 16:14:45.068837+0800 NotificationQueuesDemo[57810:12931076] 
 通知2加入数组 1 ----- <NSThread: 0x60400046e640>{number = 5, name = (null)} 
2018-11-18 16:14:45.069003+0800 NotificationQueuesDemo[57810:12931074] 
 串行队列 发出 通知2 数据 ----- <NSThread: 0x6000004625c0>{number = 4, name = (null)} 
2018-11-18 16:14:45.069276+0800 NotificationQueuesDemo[57810:12931074] 
 收到 通知2 数据 ----- <NSThread: 0x6000004625c0>{number = 4, name = (null)} 
2018-11-18 16:14:45.069536+0800 NotificationQueuesDemo[57810:12931074] 
 串行队列 结束 通知2  ----- <NSThread: 0x6000004625c0>{number = 4, name = (null)} 
2018-11-18 16:14:45.069538+0800 NotificationQueuesDemo[57810:12931076] 
 通知2加入数组 2 ----- <NSThread: 0x60400046e640>{number = 5, name = (null)} 
2018-11-18 16:14:45.069590+0800 NotificationQueuesDemo[57810:12931075] 
 代理回调收到 通知2 数据 2个   ------ <NSThread: 0x60000027d640>{number = 3, name = (null)} 

   

demo 地址: https://github.com/MadahaCEO/NotificationQueue

 

  

posted on 2018-11-18 12:38  马大哈哈  阅读(379)  评论(0编辑  收藏  举报

导航