Notification 到底怎么玩 ?

请一定注意log中打印时间 & 线程

主线程注册通知

- (instancetype)init {
    
    self = [super init];
    if (self) {
        
        [self addNotifications];
    }
    return self;
}

- (void)addNotifications {
    
    NSLog(@"\n 注册 通知  ----- %@ ", [NSThread currentThread]);
    
    __weak typeof(self) weakSelf = self;
    /*
     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 {
    
    sleep(1.0);
    NSLog(@"\n 收到 通知1 数据 ----- %@ ", [NSThread currentThread]);
}

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

串行、并行、主队列分别发送通知

  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]);
        }
    });

控制台打印结果如下

1.串行发通知,睡眠1s后,收到通知log才打印。这 1s 模拟某种场景,然后继续发通知后续代码,但是不会卡主线程

2.并行队列发通知,睡眠1s后,收到通知log才打印。

3.主队列发通知,睡眠1s后,收到通知log才打印。

猜你喜欢

转载自www.cnblogs.com/madaha/p/9977537.html