美文网首页
NSNotification 深入探究

NSNotification 深入探究

作者: cxlhaha | 来源:发表于2018-10-18 16:09 被阅读14次

    使用背景

    通常情况下,我们一个对象想要调用另外一个对象的方法,便是向这个对象传递消息,那么有个前提是,我们需要能找到这个对象,或者和这个对象有联系(这个对象传递给我一个block,或者是我的观察者等)。

    那如果这响应消息的对象和我们当前对象并没有什么关系,处于两个独立的子系统,或者我们不知道它是谁,有几个对象需要相应这个消息,那么通知机制就应运而生,它就是为了处理这样的情况。

    常规使用

    我们在最常用的通知使用方式中,一般为以下三个步骤:

    1.添加通知的观察者和相应方法

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(reciveNotification:) name:@"test" object:nil];
    
    -(void)reciveNotification:(NSNotification *)notification{
        NSLog(@"%@",notification);
    }
    

    2.发送通知

    [[NSNotificationCenter defaultCenter] postNotificationName:@"test" object:nil userInfo:@{@"k":@"v"}];
    

    3.移除通知

    -(void)dealloc
    {
        [[NSNotificationCenter defaultCenter] removeObserver:self];
    }
    

    相信这种常规用法,同学们经常使用,但是有几个注意点,我这里要提一下。

    • 发送通知和接受通知ABI中的object参数,是通知发送者的意思。如果添加观察者的时候指定了object,也就是指定了发送者,那么只接受这个发送者发送的通知。如果发送通知时没有指定发送者或者指定了其它发送者,那么不会接受响应这个通知。如果添加观察者时没有指定发送者object,那么,任何发送者发送的通知,只要name匹配就都会响应。

    • 关于移除通知,早在iOS8以及iOS8之前,如果不在dealloc时候移除观察者的话,当该通知再次发送时,会因为bad_access(野指针错误)而崩溃,因为当时通知中心持有的观察者被修饰为unsafe_unretained,当观察者被销毁后就会指向一块空白的内存区域,当通知中心对这个对象发送消息时,就会野指针错误而崩溃;而在iOS9以后,当observe将要被释放时,系统帮助我们判断,如果我们没有移除观察者的话,则系统帮助我们移除观察者。并且,在iOS9以后,通知中心保存的观察者被修饰为weak,当观察者被销毁后,该指针指向nil,我们知道,对nil发送消息是不会有任何问题的,只是,也没有任何效果而已。

    多线程相关

    我们编写如下代码:

    //发送
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_global_queue(0, 0), ^{
            NSLog(@"willSent ---- %@",[NSThread currentThread]);
            [[NSNotificationCenter defaultCenter] postNotificationName:@"test" object:nil userInfo:@{@"k":@"v"}];
            NSLog(@"sent ---- %@",[NSThread currentThread]);
        });
        
    //接收
        -(void)reciveNotification:(NSNotification *)notification{
        sleep(3);
        NSLog(@"%@---%@",notification,[NSThread currentThread]);
    }
    

    控制台打印如下:

    2018-10-18 14:08:14.934069+0800 NSNotificationTest[70998:5916452] willSent ---- <NSThread: 0x600001648e00>{number = 3, name = (null)}
    2018-10-18 14:08:17.934687+0800 NSNotificationTest[70998:5916452] NSConcreteNotification 0x600000d40930 {name = test; userInfo = {
        k = v;
    }}---<NSThread: 0x600001648e00>{number = 3, name = (null)}
    2018-10-18 14:08:17.934870+0800 NSNotificationTest[70998:5916452] sent ---- <NSThread: 0x600001648e00>{number = 3, name = (null)}
    

    通过以上的简单的测试代码,我们可以得到以下结论:

    1. 发送通知和接受通知时响应通知的方法总是在同一个线程。
    2. 所以通知是会阻塞当前线程,当响应通知的方法处理完以后,代码才会继续执行。

    多次添加的观察者

    如果我们重复多次添加观察者,那么通知的响应方法会多次调用:

    for (int i = 0; i<10; i++) {
            [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(reciveNotification:) name:@"test" object:nil];
        }
    

    控制台:

    2018-10-18 15:18:01.744625+0800 NSNotificationTest[71756:5954522] NSConcreteNotification 0x6000038101e0 {name = test; userInfo = {
        k = v;
    }}---<NSThread: 0x6000023593c0>{number = 1, name = main}
    2018-10-18 15:18:01.745118+0800 NSNotificationTest[71756:5954522] NSConcreteNotification 0x6000038101e0 {name = test; userInfo = {
        k = v;
    }}---<NSThread: 0x6000023593c0>{number = 1, name = main}
    2018-10-18 15:18:01.745414+0800 NSNotificationTest[71756:5954522] NSConcreteNotification 0x6000038101e0 {name = test; userInfo = {
        k = v;
    }}---<NSThread: 0x6000023593c0>{number = 1, name = main}
    2018-10-18 15:18:01.745580+0800 NSNotificationTest[71756:5954522] NSConcreteNotification 0x6000038101e0 {name = test; userInfo = {
        k = v;
    }}---<NSThread: 0x6000023593c0>{number = 1, name = main}
    2018-10-18 15:18:01.745725+0800 NSNotificationTest[71756:5954522] NSConcreteNotification 0x6000038101e0 {name = test; userInfo = {
        k = v;
    }}---<NSThread: 0x6000023593c0>{number = 1, name = main}
    2018-10-18 15:18:01.745874+0800 NSNotificationTest[71756:5954522] NSConcreteNotification 0x6000038101e0 {name = test; userInfo = {
        k = v;
    }}---<NSThread: 0x6000023593c0>{number = 1, name = main}
    2018-10-18 15:18:01.746025+0800 NSNotificationTest[71756:5954522] NSConcreteNotification 0x6000038101e0 {name = test; userInfo = {
        k = v;
    }}---<NSThread: 0x6000023593c0>{number = 1, name = main}
    2018-10-18 15:18:01.746175+0800 NSNotificationTest[71756:5954522] NSConcreteNotification 0x6000038101e0 {name = test; userInfo = {
        k = v;
    }}---<NSThread: 0x6000023593c0>{number = 1, name = main}
    2018-10-18 15:18:01.746391+0800 NSNotificationTest[71756:5954522] NSConcreteNotification 0x6000038101e0 {name = test; userInfo = {
        k = v;
    }}---<NSThread: 0x6000023593c0>{number = 1, name = main}
    2018-10-18 15:18:01.750810+0800 NSNotificationTest[71756:5954522] NSConcreteNotification 0x6000038101e0 {name = test; userInfo = {
        k = v;
    }}---<NSThread: 0x6000023593c0>{number = 1, name = main}
    2018-10-18 15:18:01.750928+0800 NSNotificationTest[71756:5954522] sent ---- <NSThread: 0x6000023593c0>{number = 1, name = main}
    
    

    block形式添加观察者

    在通知中心中,我们还有一个以block形式响应通知的ABI:

    self.observer =   [[NSNotificationCenter defaultCenter]addObserverForName:@"test" object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
          NSLog(@"useBlock");
        }];
    

    别的参数不做说明,这个方法返回添加的观察者由系统分发,我们应该保留下来,以便移除该观察者,如果我们不主动移除它并不能像控制器,在销毁的时候我们系统帮我们移除控制器这个观察者,从而会造成内存泄露。

    相关文章

      网友评论

          本文标题:NSNotification 深入探究

          本文链接:https://www.haomeiwen.com/subject/julszftx.html