ios的notification机制是同步的
来源:互联网 发布:计算机二级vb题型 编辑:程序博客网 时间:2024/04/28 01:37
与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作为组件间解耦的方式,而不是利用它来实现异步处理
0 0
- ios的notification机制是同步的
- ios的notification机制是同步的还是异步的
- IOS的同步处理机制
- iOS Notification 的使用
- iOS Notification 的使用
- Linux Notification 机制的分析
- 关于PureMVC的notification机制
- Linux Notification 机制的分析
- Linux Notification 机制的分析
- Linux Notification 机制的分析
- 同步多线程的同步机制
- 线程的同步机制
- PDNS的同步机制
- 多线程的同步机制
- java的同步机制
- Linux的同步机制
- 进程同步的机制
- Gstreamer的同步机制
- Hadoop 执行HelloWorld, 进一步执行对HDFS中的文件查询
- Web service failed to start because of wrong IIS configuration
- 一次ORA-15055 ORA-12547 RAC中一个节点不能识别asm磁盘解决办法
- C++语言实现的WebService开发库—gsoap应用介绍
- 微信公共服务平台开发(.Net 的实现)8-------处理图片(上传下载发送)
- ios的notification机制是同步的
- 微软2月12日发布7个安全补丁
- 修改adb ,免除多个设备的烦扰。
- 微软发布安全通告 2934088
- C++语言实现的WebService开发库—gsoap应用实例
- [模拟]All in All uva10340
- ubuntu添加root用户
- 用hashmap做的一个equals、hashcode的一个小实验
- verilog中状态机的写法