NSNotificationQueue多线程
来源:互联网 发布:知乎如何收藏文章 编辑:程序博客网 时间:2024/04/29 22:31
首先上代码:
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [button setFrame:CGRectMake(0, 0, 400, 60)]; [button addTarget:self action:@selector(buttonDown) forControlEvents:UIControlEventTouchUpInside]; [button setTitle:@"Post Notification" forState:UIControlStateNormal]; [self.view addSubview:button]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(actionNotification:) name:kNotificationName object:nil]; }); }- (void) actionNotification: (NSNotification*)notification{ NSString* message = notification.object; NSLog(@"%@",message); sleep(5); NSLog(@"Action Notification Finish"); }- (void)buttonDown{ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSNotification *notification = [NSNotification notificationWithName:kNotificationName object:@"object"]; [[NSNotificationQueue defaultQueue] enqueueNotification:notification postingStyle:NSPostASAP]; }); NSLog(@"buttonDown"); }
打印信息如下:
2015-03-17 18:47:20.131 AppTest[19507:206469] buttonDown
也就是说,观察者们和poster有可能都在不同的子线程中,这样观察者们就收不到通知事件,
这里有个解决方法是把通知队列放主线程中去;
还有个解决方法就是把NSPostASAP改为NSPostNow。
0 0
- NSNotificationQueue多线程
- NSNotificationQueue
- 异步 Notification 之 NSNotificationQueue
- 多线程
- 多线程
- 多线程
- 多线程
- 多线程
- 多线程
- 多线程
- 多线程
- 多线程
- 多线程
- 多线程
- 多线程
- 多线程
- 多线程
- 多线程
- hdu 5012(bfs)
- 在Ubuntu 14.04 64bit上搭建单机本地节点Spark 1.3.0环境
- 第二周--课后实践:阅读程序(2)
- 例题3.4 K个最小和 UVa11997
- 大型网站技术架构
- NSNotificationQueue多线程
- 关于OC对象若干问题
- Find Peak Element
- libgdx 游戏设计思路
- 初识STL vector
- 16进制转8进制(较为巧妙的做法)
- 胡晓燕作业
- CSS随笔
- Memcache内存分配策略 page chunk slat