iOS知识收藏iOSiOS开发常用知识点

iOS 接收不到通知的处理办法

2017-06-02  本文已影响2700人  ZYiDa

接收收不到通知可能是因为通知的name不匹配,也有可能是接收通知的对象被提早释放。但是还有一种就是下面的情况,先看代码:

ViewController.m

#pragma mark 发送通知
- (IBAction)pushVC:(id)sender
{
    [self.navigationController pushViewController:self.second animated:YES];
    NSDictionary *userInfo = @{@"name":@"Notification",@"age":@"18",@"height":@"188cm"};
    [[NSNotificationCenter defaultCenter] postNotificationName:@"zzz"
                                                        object:nil
                                                      userInfo:userInfo];
}

ViewController pushSecondViewController,在push的时候发送一个通知,然后在SecondViewController中接收通知

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    NSLog(@"Thread:%@",[NSThread currentThread]);
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(lookupTheInfo:)
                                                 name:@"zzz"
                                               object:nil];
}

- (void)lookupTheInfo:(NSNotification *)noti
{
    NSDictionary *userInfo = noti.userInfo;
    NSLog(@"UserInfo:%@",userInfo);
}

无论在push操作前或后发送通知,当pushSecondViewController中时,是接收不到通知的。
通过NSLog(@"Thread:%@",[NSThread currentThread]);[NSOperationQueue currentQueue]我们可以查到push时的线程和接收通知时的线程为主线程,输出:Thread:<NSThread: 0x170072d80>{number = 1, name = main}
接着再看一下苹果官方的说明:

Regular notification centers deliver notifications on the thread in which the notification was posted. Distributed notification centers deliver notifications on the main thread. At times, you may require notifications to be delivered on a particular thread that is determined by you instead of the notification center. For example, if an object running in a background thread is listening for notifications from the user interface, such as a window closing, you would like to receive the notifications in the background thread instead of the main thread. In these cases, you must capture the notifications as they are delivered on the default thread and redirect them to the appropriate thread.

百度翻译了一下:

定期通知中心在通知发布的线程上提供通知。分布式通知中心在主线程上提供通知。有时,您可能需要在由您决定的特定线程上传递通知,而不是通知中心。例如,如果运行在后台线程中的对象正在侦听来自用户界面的通知,如窗口关闭,则希望接收后台线程中的通知而不是主线程。在这些情况下,您必须捕获通知,因为它们在默认线程上传递,并将它们重定向到相应的线程。

如上,也就是,通知接收的线程是基于通知发送的线程。如果接收不到通知方发送的消息,很有可能是因为它们不在同一个线程上。因此,我们可以把通知的发送方放到和接收方同一个线程中。
发送通知时做如下操作,解决了push时发送通知接收不到的情况。

    NSLog(@"Thread:%@",[NSThread currentThread]);

    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
        [self.navigationController pushViewController:self.second animated:YES];
    }];

    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
        NSDictionary *userInfo = @{@"name":@"Notification",@"age":@"18",@"height":@"188cm"};
        [[NSNotificationCenter defaultCenter] postNotificationName:@"zzz"
                                                            object:nil
                                                          userInfo:userInfo];
    }];

控制台的输出;

2017-06-02 11:40:54.689314+0800 Notification[5634:1677061] Thread:<NSThread: 0x174076000>{number = 1, name = main}
2017-06-02 11:40:54.722245+0800 Notification[5634:1677061] Thread:<NSThread: 0x174076000>{number = 1, name = main}
2017-06-02 11:40:54.766169+0800 Notification[5634:1677061] UserInfo:{
    age = 18;
    height = 188cm;
    name = Notification;
}

最后,有一点要注意,添加通知接受者和移除通知接受者的操作是成对的。如下:

- (void)dealloc
{
    /*
     *移除所有通知
    [[NSNotificationCenter defaultCenter] removeObserver:self];
     */

    /*
     *移除指定通知
     */
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"zzz" object:nil];
}

上一篇下一篇

猜你喜欢

热点阅读