https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Notifications/Articles/Threading.html#//apple_ref/doc/uid/20001289-CEGJFDFG
Delivering Notifications To Particular Threads
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.
One way to redirect notifications is to use a custom notification queue (not an NSNotificationQueue
object) to hold any notifications that are received on incorrect threads and then process them on the correct thread. This technique works as follows. You register for a notification normally. When a notification arrives, you test whether the current thread is the thread that should handle the notification. If it is the wrong thread, you store the notification in a queue and then send a signal to the correct thread, indicating that a notification needs processing. The other thread receives the signal, removes the notification from the queue, and processes the notification.
主要内容说的是,通常情况下, 在哪个线程用 NSNotificationCenter 发出消息,就会在哪个线程里调用消息。如果想打破这个规定,就要用自定义的NSNotificationQueue去处理逻辑。
我们现在来实验一下,看看实际情况,看代码:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(test) name:@"test" object:nil];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:@"test" object:nil];
NSLog(@"end........");
});
}
- (void)test{
for(int i = 0;i<10000;i++){
NSLog(@"1111");
}
NSLog(@"current thread is %@",[NSThread currentThread]);
}
log输入如下:
2015-11-26 13:23:35.231 aaaa[913:19814] 1111
2015-11-26 13:23:35.231 aaaa[913:19814] 1111
2015-11-26 13:23:35.231 aaaa[913:19814] 1111
2015-11-26 13:23:35.231 aaaa[913:19814] 1111
2015-11-26 13:23:35.231 aaaa[913:19814] 1111
2015-11-26 13:23:35.232 aaaa[913:19814] 1111
2015-11-26 13:23:35.240 aaaa[913:19814] 1111
2015-11-26 13:23:35.240 aaaa[913:19814] current thread is <NSThread: 0x79f96a30>{number = 3, name = (null)}
2015-11-26 13:23:35.240 aaaa[913:19814] end........
的确是在发出的线程执行的代码。
注意 end....的输出位置,是在整个test方法执行完毕之后的!postNotification 触发的方法竟然是同步阻塞的,这更好地解释了“在哪个线程发出,就在哪个线程调用”这句话!和runloop是2种机制,没什么关系,不要混淆!