iOS开发基础120-通知与线程

NSNotificationCenter 是 iOS 和 macOS 开发中用于消息传递的机制,可以在多个对象之间实现解耦的事件通知。理解 NSNotificationCenter 的线程模型对正确使用这一工具至关重要。

NSNotificationCenter 的线程模型

1. 消息发送线程

当你通过 NSNotificationCenter 发送消息时,消息会在调用 postNotification: 的当前线程中同步传递给所有注册的观察者。也就是说,观察者的方法在发送通知的线程中被调用。

[[NSNotificationCenter defaultCenter] postNotificationName:@"MyNotification" object:nil];

假设这段代码在子线程中执行,那么所有监听 MyNotification 通知的观察者方法都会在同一个子线程中执行。

2. 观察者方法线程

正如之前所提到的,NSNotificationCenter 的默认行为是在发布通知的线程中执行其观察者方法。因此,如果你在主线程发送通知,观察者方法将在主线程中执行;如果在子线程发送通知,观察者方法将在子线程执行。

3. 特殊考虑

由于 NSNotificationCenter 的这种默认行为,有几个重要的点需要注意:

  1. UI 更新:UI 更新必须在主线程上发生。如果你的观察者方法需要更新 UI,你需要确保它们在主线程上执行。
  2. 线程安全:如果你的通知处理代码需要访问共享资源(如单例对象、数据库等),需要确保线程安全。

在主线程处理通知

如果你需要在主线程上处理通知(而通知可能是从子线程发送的),你可以使用以下方法:

方法 1. 使用 dispatch_async

在观察者方法中,将处理代码转移到主线程上:

[[NSNotificationCenter defaultCenter] addObserverForName:@"MyNotification" object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
    dispatch_async(dispatch_get_main_queue(), ^{
        // 更新 UI 或者执行其他需要在主线程上的操作
    });
}];

方法 2. 使用 OperationQueue.main 添加观察者

你可以在添加观察者时指定一个消息处理队列:

[[NSNotificationCenter defaultCenter] addObserverForName:@"MyNotification" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
    // 更新 UI 或者执行其他需要在主线程上的操作
}];

在这种情况下,通知处理将始终在主线程(主队列)上执行。

示例

以下是一个完整的示例,展示如何在不同线程上发送通知,并确保在主线程上处理通知。

#import "ViewController.h"

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // 注册观察者,确保在主线程上处理通知
    [[NSNotificationCenter defaultCenter] addObserverForName:@"MyNotification" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
        NSLog(@"Received notification on thread: %@", [NSThread currentThread]);
        // 更新 UI
        self.view.backgroundColor = [UIColor redColor];
    }];

    // 在子线程中发布通知
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSLog(@"Posting notification from thread: %@", [NSThread currentThread]);
        [[NSNotificationCenter defaultCenter] postNotificationName:@"MyNotification" object:nil];
    });
}

@end

在这个示例中,我们在子线程中发布通知,但是通过 NSOperationQueue mainQueue 确保了通知处理逻辑在主线程上执行。

总结

NSNotificationCenter 默认在发布通知的线程上同步地将通知分发给所有观察者方法。为了确保在主线程上执行任务(例如 UI 更新),你需要明确地将任务分派到主线程。这可以通过 GCD (dispatch_async(dispatch_get_main_queue(), ^{})) 或者在添加观察者时指定 NSOperationQueue mainQueue 来实现。

理解 NSNotificationCenter 的线程行为对于编写线程安全、响应迅速的应用程序至关重要。在处理通知时,尤其是那些可能涉及到 UI 更新的通知时,更需特别注意确保这些处理在正确的线程上进行。

posted @   Mr.陳  阅读(49)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· NetPad:一个.NET开源、跨平台的C#编辑器
· PowerShell开发游戏 · 打蜜蜂
· 凌晨三点救火实录:Java内存泄漏的七个神坑,你至少踩过三个!
历史上的今天:
2015-07-17 iOS开发基础10-UIButton内边距和图片拉伸模式
2015-07-17 iOS开发基础9-提示框(UIAlertController)
点击右上角即可分享
微信分享提示