#import "ViewController.h"
@interface ViewController ()
@property(nonatomic,weak)id observer ;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
/**
监听通知
ForName:通知名字
object:谁发出的通知
queue:决定block在哪个线程执行,nil:在发布通知的线程中执行 [NSOperationQueue mainQueue]:一般都是使用主队列
usingBlock:block回调
*/
self.observer = [[NSNotificationCenter defaultCenter]addObserverForName:@"note" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
NSLog(@"监听到通知%@---",[NSThread currentThread]);
}];
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
dispatch_async(dispatch_get_global_queue(0, 0), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:@"note" object:nil];
});
}
-(void)dealloc
{
[[NSNotificationCenter defaultCenter]removeObserver:_observer];
}
@end