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

标签 ios objective-c xcode

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

相关文章:

c# - Xamarin Forms iOS 应用程序在突然启动时崩溃

ios - 跨关系解析 PFQuery

ios - 我试图在 Xcode Run Script 构建阶段返回一个非零退出代码,但它不起作用

objective-c - Class 的 isa 字段指向自身有什么意义呢?或者为什么 Xcode 费心显示这个?

objective-c - 安装 xcode 3.2 后抛出异常

objective-c - 时间以毫秒为典型时间格式

javascript - PhoneGap 只使用 UIWebVIew 还是做更多的事情?硝基?

ios - 文本字段被键盘隐藏

iOS 导航栏出现褪色

ios - Objective C 如何在类方法中引用 View