ios跨线程通知_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

链接:https://www.jianshu.com/p/2c0b863c202f

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值