NSNotification通知

14 篇文章 0 订阅
A notification center 消息中心,是消息的控制中心,所有发送的消息都是由通知中心来控制分发的。notification的信息被被包装在NSNotification对象里面。cocoa包含两种消息中心NSNotificationCenter和NSDistributedNotificationCenter。
NSNotificationCenter:每个进程都有一个默认通知中心可以通过defaultCenter获取消息通知单例对象。这个通知中心在一个单一进程中处理。在同一个设备上不同进程之间进行通信需要使用。

NSDistributedNotificationCenter。在一个多线程应用中。消息总是在所发送的线程中,可能没有和观察着注册在一个线程。每个进程都有一个默认通知中心可以通过defaultCenter获取消息通知单例对象。这个分发通知中心可以处理在不同进程中的消息。当然发送一个分布式消息开销是很大的。

1:NSNotification同步通知,发送者要在所有监听者接收处理之后才会继续执行接下来任务,可能造成一定等待延迟。

2:NSNOtificationQueue异步通知:NSNOtificationQueue看做像一个通知中心实例的缓冲区,被通知中心放进通知队列里面发送的通知会延迟到当前在runloop中运行的发送的通知结束或者run loop闲置。

样例:
同步消息
//在一个类A中注册观察者

- (id)init{
    self = [super init];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(postA:) name:@"test" object:nil];
    }
    return self;
}

- (void)postA:(NSNotification*)notice{
    id obj = notice.object;
    NSLog(@"objA = %@",obj);
}

//同理在类B中也注册监听事件
- (id)init{
    self = [super init];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(postB:) name:@"test" object:nil];
    }
    return self;
}

- (void)postB:(NSNotification*)notice{
    id obj = notice.object;
    NSLog(@"objB = %@",obj);
}

//在控制器viewDidLoad发送消息时候
TFA *a = [[TFA alloc] init];
TFB *b = [[TFB alloc] init];



[[NSNotificationCenter defaultCenter] postNotificationName:@"test" object:nil userInfo:@{@"a": @"1"}];

NSLog(@"%@ %@ %@",a,b);
断点调试可以看到输出结果是:
2014-07-11 13:46:33.078 TFNotification[18283:60b] objA = (null)
2014-07-11 13:46:33.080 TFNotification[18283:60b] objB = (null)
2014-07-11 13:46:33.081 TFNotification[18283:60b] objC = (null)
2014-07-11 13:46:33.081 TFNotification[18283:60b] A = <TFA: 0x8f70890> B = <TFB: 0x8f70400> C = <TFC: 0x8f700a0>
先分别执行所有监听的方法之后再执行输出实例A、B、C对象。
//异步消息
- (void)test
{
    [NSRunLoop currentRunLoop];
    [[NSNotificationCenter defaultCenter] addObserver:self            selector:@selector(test1:) name:@"test1" object:self];
    [[NSNotificationCenter defaultCenter] addObserver:self            selector:@selector(test2:) name:@"test2" object:self];
    [[NSNotificationQueue defaultQueue] enqueueNotification:[NSNotification notificationWithName:@"test1" object:self]
                                               postingStyle:NSPostWhenIdle coalesceMask:NSNotificationCoalescingOnName forModes:nil];
    NSLog(@"hello notificationQueue");
   
    [[NSNotificationQueue defaultQueue] enqueueNotification:[NSNotification
                                                             notificationWithName:@"test2" object:self]
                                               postingStyle:NSPostWhenIdle coalesceMask:NSNotificationCoalescingOnName forModes:nil];
    NSInteger i = 0;
    while (i<5)
    {
        [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1.0]];
        NSLog(@"runing...");
        i++;
    }
}

- (void) test1:(NSNotification*)notification;
{
    NSLog(@"NSNotificationQueue");
}

- (void) test2:(NSNotification*)notification;
{
    NSLog(@"NSNotificationQueue1");
}
//可以看到输出结果是
2014-07-11 13:45:17.630 TFNotification[18247:60b] hello notificationQueue
2014-07-11 13:45:17.632 TFNotification[18247:60b] NSNotificationQueue
2014-07-11 13:45:17.632 TFNotification[18247:60b] NSNotificationQueue1
2014-07-11 13:45:18.632 TFNotification[18247:60b] runing...
2014-07-11 13:45:19.633 TFNotification[18247:60b] runing...
2014-07-11 13:45:20.634 TFNotification[18247:60b] runing...
2014-07-11 13:45:21.636 TFNotification[18247:60b] runing...
2014-07-11 13:45:22.637 TFNotification[18247:60b] runing…
不等监听的方法输出先执行输出hello notification 之后在输出监听的方法。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值