iOS 通知多线程的使用

一、通知使用的回顾

1.1、通知使用一

添加通知

/**  添加通知  observer:观察者  aSelector:只要一监听到通知就会调用观察者这个方法 aName:通知名称  anObject:谁发出的通知或者是一些参数  - (void)addObserver:(id)observer selector:(SEL)aSelector name:(nullable NSNotificationName)aName object:(nullable id)anObject; */[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(reciveNote) name:@"note" object:nil];

发送通知

/**  发送通知  aName:通知名称  anObject:谁发出的通知或者是一些参数  - (void)postNotificationName:(NSNotificationName:)aName object:(nullable id)anObject; */[[NSNotificationCenter defaultCenter]postNotificationName:@"note" object:nil];

接收通知的消息

-(void)reciveNote:(NSNotification *)notify{    NSLog(@"通知");}

移除通知

-(void)dealloc{    [[NSNotificationCenter defaultCenter]removeObserver:self];}

1.2、通知使用二(block的通知)

定义监听的返回值

@property (nonatomic, weak) id observe;

添加通知

/**  name:通知名称  object:谁发出的通知  queue:决定block在哪个线程执行,nil:在发布通知的线程中执行  usingBlock:只要监听到通知,就会执行该blocl  - (id )addObserverForName:(nullable NSNotificationName)name object:(nullable id)obj queue:(nullable NSOperationQueue *)queue usingBlock:(void (^)(NSNotification *note))block API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0));  注意:一定要移除通知 */self.observe = [[NSNotificationCenter defaultCenter]addObserverForName:@"note" object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {          // 只要监听到通知就会被调用     NSLog(@"当前的线程=%@",[NSThread currentThread]);          NSLog(@"%@",self);     }];

发送通知

/**  发送通知  aName:通知名称  anObject:谁发出的通知  - (void)postNotificationName:(NSNotificationName)aName object:(nullable id)anObject; */[[NSNotificationCenter defaultCenter]postNotificationName:@"note" object:nil];

移除通知

[[NSNotificationCenter defaultCenter]removeObserver:self.observe];

注意:一定要移除通知

二、通知多线程的使用

2.1、利用 1.1 的通知方式

异步 添加通知

// 监听通知:异步dispatch_async(dispatch_get_global_queue(0, 0), ^{   [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(reciveNote:) name:@"note" object:nil];});

异步  发送通知

dispatch_async(dispatch_get_global_queue(0, 0), ^{   [[NSNotificationCenter defaultCenter]postNotificationName:@"note" object:nil];});

提示:接收通知的方法里面打印的是:子线程

同步  发送通知

dispatch_sync(dispatch_get_global_queue(0, 0), ^{   [[NSNotificationCenter defaultCenter]postNotificationName:@"note" object:nil];});

提示:接收通知的方法里面打印的是:主线程

接收通知的消息

-(void)reciveNote:(NSNotification *)notify{   NSLog(@"当前接收通知的线程=%@",[NSThread currentThread]);}

提示:很多时候我们可能不知道发送通知的线程,我们需要在接收通知的方法里面进行更新UI,我们可以在接收方法里面使用主线程,如下

-(void)reciveNote:(NSNotification *)notify{    NSLog(@"当前接收通知的线程=%@",[NSThread currentThread]);    dispatch_sync(dispatch_get_main_queue(), ^{          // 在此刷新UI    });}

移除通知

-(void)dealloc{   [[NSNotificationCenter defaultCenter]removeObserver:self];}

结论:不管添加通知在主线程还是子线程,接收通知的方法所在的线程是由发送通知的线程决定的。

2.2、利用 1.2 的通知方式:和上面的一样,我直接说有关刷新的问题

self.observe = [[NSNotificationCenter defaultCenter]addObserverForName:@"note" object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {      // 只要监听到通知就会被调用      NSLog(@"当前的线程=%@",[NSThread currentThread]);      NSLog(@"%@",self);}];

分析:如果上面的是:queue 为 nil,那么block里面的线程是由发送通知的线程决定,那么如果block里面是子线程我们就无法刷新UI了,解决办法是把 nil 改为 [NSOperationQueue mainQueue],不管发送通知的线程是什么,block里面都是主线程,如下

self.observe = [[NSNotificationCenter defaultCenter]addObserverForName:@"note" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {    // 只要监听到通知就会被调用    NSLog(@"当前的线程=%@",[NSThread currentThread]);}];

最后这是调试的 demo

作者:IIronMan

作为一个开发者,有一个学习的氛围跟一个交流圈子特别重要这是一个我的iOS交流群:869685378,不管你是小白还是大牛欢迎入驻 ,分享BAT,阿里面试题、面试经验,讨论技术, 大家一起交流学习成长!

猜你喜欢

转载自blog.csdn.net/ios8988/article/details/89569712