gpt4 book ai didi

ios - 执行选择器 : didn't work properly when using dispatch_async and global_queue

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:14:44 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com