通知发送,接收不到通知的处理办法

接收不到通知的处理办法的思路

一:先检查通知的使用姿势,通知名称和代码问题

二:然后就是:发送和接受时机问题,线程问题

场景描述:
图中我的订单控制器下包含:两个控制器个人订单队员订单
点击按钮个人订单,在我的订单发送通知,在个人订单里面收到通知,处理数据。
点击按钮队员订单,在我的订单发送通知,在队员订单里面收到通知,处理数据。
我们在我的订单控制器切换的按钮点击事件里面发送通知

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

我们在两个控制器个人订单队员订单里面接受通知

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

问题:
首次进这个我的订单,然后切换到队员订单队员订单里面没有收到通知,在个人订单可以收到。来回切换几次,个人订单队员订单就都可以收到了。

image.png

通过NSLog(@"Thread:%@",[NSThread currentThread]);[NSOperationQueue currentQueue]我们可以查到push时的线程和接收通知时的线程为主线程,输出:Thread:{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:{number = 1, name = main}
2017-06-02 11:40:54.722245+0800 Notification[5634:1677061] Thread:{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];
}

你可能感兴趣的:(通知发送,接收不到通知的处理办法)