NSNotificationCenter postNotificationName同步阻塞调用
来源:互联网 发布:淘宝联盟佣金设置 编辑:程序博客网 时间:2024/05/16 18:18
今天测试了NSNotificationCenter发送消息后,在目标函数和调用函数中,功能执行顺序:
代码如下:
- (void)begin
{
for (int i = 0; i < 100; i++)
{
NSLog(@"notify:%d", i);
}
}
- (void)buttonClicked:(id)sender
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"notify" object:nil];
for (int i = 0; i < 100; i++)
{
NSLog(@"main:%d", i);
}
}
-(void)viewDidLoad
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(100, 100, 50, 80);
[button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(begin) name:@"notify" object:nil];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"buttonClicked" object:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
发现打印的顺序为:
2012-08-03 14:00:56.239 testNotifyBegin[6522:f803] notify:0
2012-08-03 14:00:56.607 testNotifyBegin[6522:f803] notify:1
2012-08-03 14:00:56.960 testNotifyBegin[6522:f803] notify:2
2012-08-03 14:00:57.283 testNotifyBegin[6522:f803] notify:3
。
。
。
2012-08-03 14:00:57.283 testNotifyBegin[6522:f803] notify:98
2012-08-03 14:00:57.283 testNotifyBegin[6522:f803] notify:99
然后才是
2012-08-03 14:11:56.089 testNotifyBegin[6522:f803] main:0
2012-08-03 14:12:00.338 testNotifyBegin[6522:f803] main:1
2012-08-03 14:12:05.220 testNotifyBegin[6522:f803] main:2
2012-08-03 14:12:06.368 testNotifyBegin[6522:f803] main:3
2012-08-03 14:12:07.077 testNotifyBegin[6522:f803] main:4
。
。
。
2012-08-03 14:12:06.368 testNotifyBegin[6522:f803] main:98
2012-08-03 14:12:07.077 testNotifyBegin[6522:f803] main:99
所以说,NSNotificationCenter在post消息后,会一直调用函数中会一直等待被调用函数执行完全,然后返回控制权到主函数中,再接着执行后面的功能。即:这是一个同步阻塞的操作。
如果要想不等待,直接返回控制权,可以采用NSNotificationQueue。
代码如下:
- (void)begin
{
for (int i = 0; i < 100; i++)
{
NSLog(@"notify:%d", i);
}
}
- (void)buttonClicked:(id)sender
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"notify" object:nil];
for (int i = 0; i < 100; i++)
{
NSLog(@"main:%d", i);
}
}
-(void)viewDidLoad
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(100, 100, 50, 80);
[button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(begin) name:@"notify" object:nil];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"buttonClicked" object:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
发现打印的顺序为:
2012-08-03 14:00:56.239 testNotifyBegin[6522:f803] notify:0
2012-08-03 14:00:56.607 testNotifyBegin[6522:f803] notify:1
2012-08-03 14:00:56.960 testNotifyBegin[6522:f803] notify:2
2012-08-03 14:00:57.283 testNotifyBegin[6522:f803] notify:3
。
。
。
2012-08-03 14:00:57.283 testNotifyBegin[6522:f803] notify:98
2012-08-03 14:00:57.283 testNotifyBegin[6522:f803] notify:99
然后才是
2012-08-03 14:11:56.089 testNotifyBegin[6522:f803] main:0
2012-08-03 14:12:00.338 testNotifyBegin[6522:f803] main:1
2012-08-03 14:12:05.220 testNotifyBegin[6522:f803] main:2
2012-08-03 14:12:06.368 testNotifyBegin[6522:f803] main:3
2012-08-03 14:12:07.077 testNotifyBegin[6522:f803] main:4
。
。
。
2012-08-03 14:12:06.368 testNotifyBegin[6522:f803] main:98
2012-08-03 14:12:07.077 testNotifyBegin[6522:f803] main:99
所以说,NSNotificationCenter在post消息后,会一直调用函数中会一直等待被调用函数执行完全,然后返回控制权到主函数中,再接着执行后面的功能。即:这是一个同步阻塞的操作。
如果要想不等待,直接返回控制权,可以采用NSNotificationQueue。
- NSNotificationCenter postNotificationName同步阻塞调用
- NSNotificationCenter postNotificationName同步阻塞调用
- postNotificationName是个同步调用
- postNotificationName同步调用导致的白屏问题
- iOS: NSNotificationCenter的方法postNotificationName:object:
- iOS: NSNotificationCenter的方法postNotificationName:object:userInfo:
- postNotificationName
- NSNotificationCenter 是同步
- NSNotificationCenter 是同步
- iphone 消息处理事件 postNotificationName和addObserver的调用
- iphone 消息处理事件 postNotificationName和addObserver的调用
- 同步,阻塞
- NSNotificationCenter的同步和异步
- NSNotificationCenter同步异步的问题
- thrift java多线程非阻塞同步/异步调用实例
- 网络编程中同步、异步、阻塞和非阻塞四种调用方式的区别
- 同步阻塞, 同步非阻塞,异步阻塞,异步非阻塞
- 同步、异步、阻塞、非阻塞
- ubuntu默认root密码
- uva 539 The Settlers of Catan
- Oracle 中获取日期中的某部分值
- POJ 1002 487-3279(排序or不排序)
- Activity设置无标题栏效果和全屏效果
- NSNotificationCenter postNotificationName同步阻塞调用
- 游戏内实现手机摇一摇分析
- dedecms 调用多个栏目的用法
- Can not find the tag library descriptor for "http://java.sun.com/j...
- 定义非static的未初始化的全局变量是流氓行为
- Tomcat开机启动
- CVU介绍
- OpenStack使用Bosh部署CloudFoundry(一)—准备OpenStack环境
- openfire学习4------->android客户端聊天开发之聊天功能开发