iOS-NSNotificationCenter通知中心是同步操作还是异步操作
2017-04-24 15:11
316 查看
前言
最近有个小伙伴到喜马拉雅去面试,面试官问他一个问题就是“通知中心是同步操作还是异步操作?”,小伙伴回答成异步了,然后就是回家等消息,然后就是没有然后了。。。
我先举几个小的列子给大家瞅瞅:
发送通知
- (void)sentValueBtnClick:(UIButton *)button{
NSLog(@"发送通知");
NSDictionary *dict = @{@"myValue":@"ZFJ通知传值"};
[[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:@"KPassValue" object:nil userInfo:dict]];
}
接收通知
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_textField.layer.borderColor = [UIColor redColor].CGColor;
_textField.layer.borderWidth = 1.0;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(passValue:) name:@"KPassValue" object:nil];
}
- (void)passValue:(NSNotification *)text{
NSString *valueStr = text.userInfo[@"myValue"];
NSLog(@"收到值:%@",valueStr);
sleep(3);
self.textField.text = valueStr;
NSLog(@"通知赋值完毕");
}
打印结果:
2017-04-24 14:36:42.043 NSNotification[8150:145628] 发送通知2017-04-24 14:36:42.043 NSNotification[8150:145628] 收到值:ZFJ通知传值2017-04-24 14:36:45.044 NSNotification[8150:145628] 通知赋值完毕
案例分析
通过打印我们可以看出,当我们发送通知以后,观察者在接收到值以后,我们休眠3秒,程序才会继续往下执行,也就是说这个过程是同步的;我认为这里面设计为同步,是考虑到这一点,那就是一个通知可能有多个监听者,采用同步的方式能够保证所有的观察者都能够对通知做出相应,不会遗漏。
异步操作
如果我们想异步操作怎么办呢?莫急且看下面:
发送通知:
- (void)sentValueBtnClick:(UIButton *)button{
NSLog(@"发送通知");
NSDictionary *dict = @{@"myValue":@"ZFJ通知传值"};
dispatch_async(dispatch_get_global_queue(0, 0), ^{
[[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:@"KPassValue" object:nil userInfo:dict]];
});
}
接受通知:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_textField.layer.borderColor = [UIColor redColor].CGColor;
_textField.layer.borderWidth = 1.0;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(passValue:) name:@"KPassValue" object:nil];
}
- (void)passValue:(NSNotification *)text{
NSString *valueStr = text.userInfo[@"myValue"];
NSLog(@"收到值:%@",valueStr);
dispatch_async(dispatch_get_global_queue(0, 0), ^{
sleep(3);
self.textField.text = valueStr;
NSLog(@"通知赋值完毕");
});
}
我们发布通知在子线程里面发布,当然接收也要在子线程里面接收了。。。
欢迎大家讨论和补充。。。
最近有个小伙伴到喜马拉雅去面试,面试官问他一个问题就是“通知中心是同步操作还是异步操作?”,小伙伴回答成异步了,然后就是回家等消息,然后就是没有然后了。。。
我先举几个小的列子给大家瞅瞅:
发送通知
- (void)sentValueBtnClick:(UIButton *)button{
NSLog(@"发送通知");
NSDictionary *dict = @{@"myValue":@"ZFJ通知传值"};
[[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:@"KPassValue" object:nil userInfo:dict]];
}
接收通知
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_textField.layer.borderColor = [UIColor redColor].CGColor;
_textField.layer.borderWidth = 1.0;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(passValue:) name:@"KPassValue" object:nil];
}
- (void)passValue:(NSNotification *)text{
NSString *valueStr = text.userInfo[@"myValue"];
NSLog(@"收到值:%@",valueStr);
sleep(3);
self.textField.text = valueStr;
NSLog(@"通知赋值完毕");
}
打印结果:
2017-04-24 14:36:42.043 NSNotification[8150:145628] 发送通知2017-04-24 14:36:42.043 NSNotification[8150:145628] 收到值:ZFJ通知传值2017-04-24 14:36:45.044 NSNotification[8150:145628] 通知赋值完毕
案例分析
通过打印我们可以看出,当我们发送通知以后,观察者在接收到值以后,我们休眠3秒,程序才会继续往下执行,也就是说这个过程是同步的;我认为这里面设计为同步,是考虑到这一点,那就是一个通知可能有多个监听者,采用同步的方式能够保证所有的观察者都能够对通知做出相应,不会遗漏。
异步操作
如果我们想异步操作怎么办呢?莫急且看下面:
发送通知:
- (void)sentValueBtnClick:(UIButton *)button{
NSLog(@"发送通知");
NSDictionary *dict = @{@"myValue":@"ZFJ通知传值"};
dispatch_async(dispatch_get_global_queue(0, 0), ^{
[[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:@"KPassValue" object:nil userInfo:dict]];
});
}
接受通知:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_textField.layer.borderColor = [UIColor redColor].CGColor;
_textField.layer.borderWidth = 1.0;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(passValue:) name:@"KPassValue" object:nil];
}
- (void)passValue:(NSNotification *)text{
NSString *valueStr = text.userInfo[@"myValue"];
NSLog(@"收到值:%@",valueStr);
dispatch_async(dispatch_get_global_queue(0, 0), ^{
sleep(3);
self.textField.text = valueStr;
NSLog(@"通知赋值完毕");
});
}
我们发布通知在子线程里面发布,当然接收也要在子线程里面接收了。。。
欢迎大家讨论和补充。。。
相关文章推荐
- iOS高级面试之NSNotificationCenter是同步操作还是异步操作
- ios的notification机制是同步的还是异步的
- NSNotificationCenter通知中心是同步操作还是异步操作
- ios的线程和同步异步操作
- ios的notification机制是同步的还是异步的
- IOS GCD的5种队列用法和异步、同步操作
- ios使用GCD来执行同步或异步串行、并行操作。
- IOS GCD的5种队列用法和异步、同步操作
- iOS-GCD学习之同步异步线程去执行串行操作
- iphone ios http 异步同步操作
- lock Mutex Monitor 之间的区别与详解, .net 多线程 同步异步操作,锁
- IOS同步请求和异步请求的区别
- Chapter10-“I/O设备的同步和异步”之文件操作相关
- [转]IOS程序之HTTP同步请求与异步请求问题解决
- 漫谈Silverlight 同步操作与异步操作的思维转换(转载)
- Ajax,选择同步还是异步
- 异步操作转同步
- 在服务器端判断request来自Ajax请求(异步)还是传统请求(同步)
- Chapter10-“I/O设备的同步和异步”之I/O设备同步操作
- CERL2 系列2:网络编程该用同步还是异步?