A notification center 消息中心,是消息的控制中心,所有发送的消息都是由通知中心来控制分发的。notification的信息被被包装在NSNotification对象里面。cocoa包含两种消息中心NSNotificationCenter和NSDistributedNotificationCenter。
NSNotificationCenter:每个进程都有一个默认通知中心可以通过defaultCenter获取消息通知单例对象。这个通知中心在一个单一进程中处理。在同一个设备上不同进程之间进行通信需要使用。
NSDistributedNotificationCenter。在一个多线程应用中。消息总是在所发送的线程中,可能没有和观察着注册在一个线程。每个进程都有一个默认通知中心可以通过defaultCenter获取消息通知单例对象。这个分发通知中心可以处理在不同进程中的消息。当然发送一个分布式消息开销是很大的。
1:NSNotification同步通知,发送者要在所有监听者接收处理之后才会继续执行接下来任务,可能造成一定等待延迟。
//在控制器viewDidLoad发送消息时候
//异步消息
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 之后在输出监听的方法。