美文网首页
通知发送,接收不到通知的处理办法

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

作者: 物非0人非 | 来源:发表于2021-11-19 11:40 被阅读0次

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

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

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

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

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

相关文章

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

    接收不到通知的处理办法的思路 一:先检查通知的使用姿势,通知名称和代码问题 二:然后就是:发送和接受时机问题,线程...

  • 创建通知-通知的用法

    1.创建通知 创建通知 通过通知中心发送通知 2.接收通知并进行相依处理 接收通知

  • NSNotification发通知接收不到

    通知的顺序是:先接收通知,再发送通知。很多时候,明明我发送通知和接收通知的方法都写了,为什么接收不到通知啊?原因很...

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

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

  • NSNotificationCenter发送通知,接收通知

    这里所说的通知不是指发给用户看的通知消息,而是系统内部进行消息传递的通知。要介绍通知之前,我们需要先了解什么是观察...

  • iOS 的通知的使用

    通知的使用 接收不到通知 首先你需要检查你发送和接收的通知名称(name)是否一致,使用类型常量可以避免这个问题 ...

  • iOS--《传值方法》之通知中心传值

    第1步:在发送者中实现一个方法进行发送通知。 第2步:在接收者中注册通知,也就是接收者要进行接收通知,接收通知和发...

  • swift的通知、代理、KVO&RXSwift键值观察、闭包(b

    一、通知 1、发送通知 2、接收通知 思考:如果我们需要通知传值呢?1、发送通知 2、接受通知,并取出数据 3、移...

  • 首次安装进入APP

    会发送 UIApplicationDidBecomeActiveNotification 通知.接收到通知可以做相...

  • iOS通知和线程

    打印结果:当在主线程发送通知时: 当在子线程发送通知时: 结论:通知在哪个线程发送,接收就在那个线程。

网友评论

      本文标题:通知发送,接收不到通知的处理办法

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