0
- (void)viewDidLoad { 
    [super viewDidLoad]; 
    //NSNotificationCenter第一坑 

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

    //在主线程中注册通知并转发消息 
    //add observer in main thread and deal in main thread 

    dispatch_async(dispatch_get_main_queue(), ^{ 
     NSLog(@"current thread:%@",[NSThread currentThread]); 
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:@"notificaiton_1" object:nil]; 
    }); 

    //在子线程中发送通知 
    //post notification in another thread not in the same thread as main 

    dispatch_async(dispatch_get_global_queue(0, 0), ^{ 

     [[NSNotificationCenter defaultCenter] postNotificationName:@"notificaiton_1" object:nil]; 

    }); 


    // Do any additional setup after loading the view, typically from a nib. 
} 
//and this method never be called 
- (void)handleNotification:(NSNotification *)notification{ 
    //处理通知 
    //handle notification here 

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

    NSLog(@"handle notification"); 

} 
+0

Зачем вы хотите это сделать? Наиболее распространенная практика - опубликовать уведомление в главной теме ... – Subbu

+0

да, напишите уведомление в главной теме, может быть, хорошая практика! но я хочу знать теорию внедрения NSNotification и почему нельзя вызывать метод уведомлений - (void) handleNotification: (NSNotification *). – lobster

ответ

0

Во-первых, вы должны знать очереди & нить совершенно разные понятия. Очередь может проходить через несколько потоков.

Во-вторых, если вы публикуете уведомление о потоке, наблюдатели вызывают эту тему.

Так что в соответствии с вашими кодами handleNotification определенно не называется.