美文网首页将来跳槽用
iOS-通知、多线程通知

iOS-通知、多线程通知

作者: 梦蕊dream | 来源:发表于2018-06-29 14:51 被阅读43次

    基本使用

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

    通知示例一:

    // 接收通知
    [[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]);
        }];
    

    相关文章

      网友评论

        本文标题:iOS-通知、多线程通知

        本文链接:https://www.haomeiwen.com/subject/iizoyftx.html