注册观察者:
主线程:
[[NSNotificationCenter defaultCenter] addObserverForName:HAHAHA object:nil queue:[NSOperationQueue mainQueue] usingBlock:completeCallback];
发送消息:
子线程:
[[NSNotificationCenter defaultCenter] postNotificationName:HAHAHA object:nil];
会发现postNotificationName方法执行了很久,大概30秒的样子,completeCallback才会被调到。
解决方法:
调用发送消息的时候,将调用代码放到主线程。
dispatch_async(dispatch_get_main_queue(), ^(){
[[NSNotificationCenter defaultCenter] postNotificationName:HAHAHA object:nil];
});
结论:NSNotificationCenter 在广播消息的时候,线程切换的开销比较大。
原因待深入查明。