Object -C 通知 &线程的注意点

2017-01-03  本文已影响13人  哔哩哔哩智能喵
#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
上一篇下一篇

猜你喜欢

热点阅读