繁体   English   中英

performSelector:使用 dispatch_async 和 global_queue 时无法正常工作

[英]performSelector: didn't work properly when using dispatch_async and global_queue

我想问一下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(), ^{});调用您的选择器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
    });
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM