NSNotification 深入探究

2018-10-18  本文已影响14人  cxlhaha

使用背景

通常情况下,我们一个对象想要调用另外一个对象的方法,便是向这个对象传递消息,那么有个前提是,我们需要能找到这个对象,或者和这个对象有联系(这个对象传递给我一个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];
}

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

多线程相关

我们编写如下代码:

//发送
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");
    }];

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

上一篇下一篇

猜你喜欢

热点阅读