全局队列中的计时器未在iOS中调用

use*_*279 5 iphone nstimer grand-central-dispatch ios

-(void)viewDidLoad{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [NSTimer scheduledTimerWithTimeInterval:0.10 
                                         target:self 
                                       selector:@selector(action_Timer) 
                                       userInfo:nil 
                                        repeats:YES];
        }
    );
}

-(void)action_Timer{
    LOG("Timer called");
}
Run Code Online (Sandbox Code Playgroud)

action_Timer没有被召唤.我不知道为什么.你有什么主意吗?

rob*_*off 16

你是+[NSTimer scheduledTimerWithTimeInterval:...]从一个GCD工作者线程调用的.GCD工作线程不运行运行循环.这就是为什么你的第一次尝试不起作用的原因.

当您尝试时[[NSRunLoop mainRunLoop] addTimer:myTimer forMode:NSDefaultRunLoopMode],您正在从GCD工作线程向主运行循环发送消息.那里的问题NSRunLoop不是线程安全的.(这在NSRunLoop类参考中有记录.)

相反,您需要调度回主队列,以便在将addTimer:...消息发送到主运行循环时,它在主线程上完成.

-(void)viewDidLoad {
    [super viewDidLoad];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSTimer *timer = [NSTimer timerWithTimeInterval:0.10 
                                         target:self 
                                       selector:@selector(action_Timer) 
                                       userInfo:nil 
                                        repeats:YES];
        dispatch_async(dispatch_get_main_queue(), ^{
            [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
        });        
    });
}
Run Code Online (Sandbox Code Playgroud)

实际上,如果您要在主运行循环中安排计时器,则没有理由在后台队列上创建计时器.您可以调度回主队列来创建和安排它:

-(void)viewDidLoad {
    [super viewDidLoad];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSLog(@"on background queue");
        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"on main queue");
            [NSTimer scheduledTimerWithTimeInterval:0.10 
                                         target:self 
                                       selector:@selector(action_Timer) 
                                       userInfo:nil 
                                        repeats:YES];
        });        
    });
}
Run Code Online (Sandbox Code Playgroud)

请注意,我的两个解决方案都将计时器添加到主运行循环中,因此计时器的操作将在主线程上运行.如果您希望计时器的操作在后台队列上运行,您应该从操作中调度它:

-(void)action_Timer {
    // This is called on the main queue, so dispatch to a background queue.
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        LOG("Timer called");
    });
}
Run Code Online (Sandbox Code Playgroud)