ios的notification机制是同步的还是异步的
来源:互联网 发布:淘宝账号异常 编辑:程序博客网 时间:2024/05/16 01:14
与javascript中的事件机制不同,ios里的事件广播机制是同步的,默认情况下,广播一个通知,会阻塞后面的代码:
- -(void) clicked
- {
- NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
- [center postNotificationName:@"event_happend" object:self];
- NSLog(@"all handler done");
- }
按下按钮后,发送一个广播,此前已经注册了2个此事件的侦听者
- -(id) init
- {
- self = [super init];
- if(self){
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(whenReceive:) name:@"event_happend" object:nil];
- }
- return self;
- }
- -(void) whenReceive:(NSNotification*) notification
- {
- NSLog(@"im1111");
- }
- -(id) init
- {
- self = [super init];
- if(self){
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(whenReceive:) name:@"event_happend" object:nil];
- }
- return self;
- }
- -(void) whenReceive:(NSNotification*) notification
- {
- NSLog(@"im22222");
- }
执行这段代码,首先会输出im1111,然后是im22222,最后才是all handler done。调试发现,代码始终是跑在同一个线程中(广播事件的线程),广播事件之后的代码被阻塞,直到所有的侦听者都执行完响应
所以,由于NotificationCenter的这个特性,如果希望广播的事件异步处理,则需要在侦听者的方法里开启新线程。应该把Notification作为组件间解耦的方式,而不是利用它来实现异步处理
1 0
- ios的notification机制是同步的还是异步的
- ios的notification机制是同步的
- setinterval是同步还是异步的问题
- 通知中心是同步的,还是异步的?
- 关于“$M.Ajax的请求是异步的还是同步的”的回答
- Qt的signal和slot是同步的,还是异步的?
- java 同步 异步 (2)-- synchronized锁住的是代码还是对象
- android的同步和异步机制
- linux驱动的异步同步通知机制
- iOS-NSNotificationCenter通知中心是同步操作还是异步操作
- IOS的同步处理机制
- NSNotification是同步还是异步?
- 关于Oracle DataGuard 同步 还是 异步 的问题
- IOS的同步下载及异步下载
- iOS线程的同步和异步
- ios的线程和同步异步操作
- [ios] dispatch_get_main_queue 的同步异步问题
- iOS多线程相关,同步异步的问题
- 不安装OpenCV Manager使用OpenCV
- UITextView的创建/背景图片/边框和颜色/圆角等
- 产品经理经常犯的十大顶级错误
- maven pom查询
- hdu 5239 Doom(线段树+数论)
- ios的notification机制是同步的还是异步的
- centos下 ifconfig的安装
- 隐马尔科夫学习五(三)
- 正则表达式30分钟入门教程
- 沉浸式状态栏调用方法
- C++中的static关键字
- Linux的IPC命令
- xml文件的解析(libxml2)
- bind和live的区别