通知發(fā)送,接收不到通知的處理辦法

接收不到通知的處理辦法的思路

一:先檢查通知的使用姿勢,通知名稱和代碼問題

二:然后就是:發(fā)送和接受時機問題,線程問題

場景描述:
圖中我的訂單控制器下包含:兩個控制器個人訂單隊員訂單。
點擊按鈕個人訂單,在我的訂單發(fā)送通知,在個人訂單里面收到通知,處理數(shù)據(jù)。
點擊按鈕隊員訂單,在我的訂單發(fā)送通知,在隊員訂單里面收到通知,處理數(shù)據(jù)。
我們在我的訂單控制器切換的按鈕點擊事件里面發(fā)送通知

 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.

百度翻譯了一下:

定期通知中心在通知發(fā)布的線程上提供通知。分布式通知中心在主線程上提供通知。有時,您可能需要在由您決定的特定線程上傳遞通知,而不是通知中心。例如,如果運行在后臺線程中的對象正在偵聽來自用戶界面的通知,如窗口關(guān)閉,則希望接收后臺線程中的通知而不是主線程。在這些情況下,您必須捕獲通知,因為它們在默認線程上傳遞,并將它們重定向到相應(yīng)的線程。
如上,也就是,通知接收的線程是基于通知發(fā)送的線程。如果接收不到通知方發(fā)送的消息,很有可能是因為它們不在同一個線程上。因此,我們可以把通知的發(fā)送方放到和接收方同一個線程中。

解決:發(fā)送通知時做如下操作,解決了push時發(fā)送通知接收不到的情況。

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];
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容