iOS-通知、多线程通知

基本使用

通知是有顺序的,先监听再发送 才会收到信息

通知示例一:

// 接收通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(indexairportName) name:@"indexairportName" object:nil];

#pragma mark ===========接受通知
-(void)indexairportName{
  /**处理通知代码**/
  }

//销毁通知
- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

//其他页面-发出通知
[[NSNotificationCenter defaultCenter] postNotificationName:@"indexairportName" object:nil];

通知示例二:
与示例一接受通知方法不同,销毁方法也不同

@property (nonatomic,weak) id observe;

/**
     * queue:block在哪个线程执行,传值 nil:在通知发布的线程中执行
     * block:接受通知后执行操作
     */

_observe = [[NSNotificationCenter defaultCenter] addObserverForName:@"" object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
      /**处理通知代码**/
}];

//销毁通知
- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:_observe];
}

//其他页面-发出通知
[[NSNotificationCenter defaultCenter] postNotificationName:@"indexairportName" object:nil];

多线程使用

示例一:多线程

  • 接收通知处理代码线程 由发出通知的线程决定
  • 发通知(主线程)-监听通知(子线程):接收通知代码在主线程处理
  • 发通知(子线程)-监听通知(主线程):接收通知代码在子线程处理
- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationCode) name:@"noti" object:nil];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [[NSNotificationCenter defaultCenter] postNotificationName:@"noti" object:nil];
    });
}
/** 监听通知会调用方法
 * 接收通知处理代码线程 由发出通知的线程决定
 * 发通知(主线程)-监听通知(子线程):接收通知代码在主线程处理
 * 发通知(子线程)-监听通知(主线程):接收通知代码在子线程处理
 */
- (void)notificationCode{
    dispatch_async(dispatch_get_main_queue(), ^{
       //更新 UI 操作
    });
    NSLog(@"notificationCode:%@",[NSThread currentThread]);
}
- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

示例二:多线程

  • 接收通知处理代码线程 由NSNotificationCenter的队列决定
  • 发通知(主线程)-监听通知(并发队列):接收通知代码在子线程处理
  • 发通知(子线程)-监听通知(主队列):接收通知代码在主线程处理
@property (nonatomic,weak) id observe;
- (void)viewDidLoad {
    [super viewDidLoad];
    _observe = [[NSNotificationCenter defaultCenter] addObserverForName:@"noti" object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
        /**处理通知代码**/
        NSLog(@"usingBlock:%@",[NSThread currentThread]);
    }];
}
- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:_observe];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [[NSNotificationCenter defaultCenter] postNotificationName:@"noti" object:nil];
    });
}

block 中回归主线程方法:
1.多线程

dispatch_async(dispatch_get_main_queue(), ^{
       //更新 UI 操作
    });

2.设置队列

_observe = [[NSNotificationCenter defaultCenter] addObserverForName:@"noti" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
        /**处理通知代码**/
        NSLog(@"usingBlock:%@",[NSThread currentThread]);
    }];
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1.ios高性能编程 (1).内层 最小的内层平均值和峰值(2).耗电量 高效的算法和数据结构(3).初始化时...
    欧辰_OSR阅读 29,664评论 8 265
  • OC语言基础 1.类与对象 类方法 OC的类方法只有2种:静态方法和实例方法两种 在OC中,只要方法声明在@int...
    奇异果好补阅读 4,359评论 0 11
  • iOS 提供了一种 “同步的” 消息通知机制NSNotificationCenter,观察者只要向消息中心注册, ...
    MasterChen阅读 2,268评论 4 16
  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,148评论 1 32
  • 《投资最重要的事》是作者霍华德马克斯倾注毕生的精力和研究给我们留下的一笔巨大财富。他完整而高度的概括了自己...
    杨胖儿阅读 825评论 3 18