作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我想问一下performSelector:方法需要自己的runloop定时器才能正常工作有什么潜在的原因,因为如果我不专门为他设置一个runloop,他就会辞职!
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"touchesBegan --- %@", [NSThread currentThread]);
dispatch_async((dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)), ^{
[self performSelector:@selector(test) withObject:nil afterDelay:2.0];
/**
* uncomment this line to make it work
*/
// [[NSRunLoop currentRunLoop] run];
});
}
-(void)test
{
NSLog(@"test --- %@", [NSThread currentThread]);
}
最佳答案
我认为您必须需要在 dispatch_async( dispatch_get_main_queue(), ^{});
中调用您的选择器。请看下面的代码。
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"touchesBegan --- %@", [NSThread currentThread]);
dispatch_async((dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)), ^{
dispatch_async( dispatch_get_main_queue(), ^{ //ADDED THIS LINE
[self performSelector:@selector(test) withObject:nil afterDelay:2.0];
//[[NSRunLoop currentRunLoop] run];
});//ADDED THIS LINE
});
}
关于ios - 执行选择器 : didn't work properly when using dispatch_async and global_queue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31042855/
我试图理解 GCD 并编写了这段代码来找出运行优先级: override func viewDidLoad() { super.viewDidLoad() fetchImage()
我想问一下performSelector:方法需要自己的runloop定时器才能正常工作有什么潜在的原因,因为如果我不专门为他设置一个runloop,他就会辞职! -(void)touchesBega
我是一名优秀的程序员,十分优秀!