我想问一下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/