iOS 通知多线程的使用

栏目: IOS · 发布时间: 5年前

内容简介:一、通知使用的回顾添加通知发送通知

一、通知使用的回顾

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


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Java解惑

Java解惑

(美)布洛克·加夫特 / 陈昊鹏 / 人民邮电出版社 / 2010-11-22 / 49.00元

你认为自己了解Java多少?你是个爱琢磨的代码侦探吗?你是否曾经花费数天时间去追踪一个由Java或其类库的陷阱和缺陷而导致的bug?你喜欢智力测验吗?本书正好适合你! Bloch和Gafter继承了Effective Java一书的传统,深入研究了Java编程语言及其核心类库的细微之处。本书特写了95个噩梦般的谜题,中间穿插着许多有趣的视觉幻象,寓教于乐。任何具备Java知识的人都可以理解这......一起来看看 《Java解惑》 这本书的介绍吧!

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器