1、主线程监听,子线程发通知
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receive) name:@"testNoti" object:nil];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"1");
dispatch_queue_t queue = dispatch_queue_create("serial", DISPATCH_QUEUE_SERIAL);
dispatch_async(queue, ^{
NSLog(@"serial %@",[NSThread currentThread]);
[[NSNotificationCenter defaultCenter] postNotificationName:@"testNoti" object:nil];
});
NSLog(@"2");
}
- (void)receive{
NSLog(@"receive %@",[NSThread currentThread]);
}
在主线程监听,子线程发通知,会在子线程接收到通知。
多线程