在runloop启动后放置定时器时,定时器的任务无法执行

时间:2016-01-13 08:25:04

标签: ios

我创建了一个新线程来学习iOS的runloop。 在我的NSThread子类中:

@implementation TestThread

-(void)main
{
    NSRunLoop *currentThreadRunLoop = [NSRunLoop currentRunLoop];
    [NSTimer scheduledTimerWithTimeInterval:1.0 target:self     selector:@selector(handleTimerTask) userInfo:nil repeats:YES];
    [currentThreadRunLoop runMode:NSDefaultRunLoopMode beforeDate:      [NSDate distantFuture]];
}

-(void) handleTimerTask
{
    NSLog(@"I am in timer task!");
}

@end

将显示该消息。但是当我在runloop启动后放置计时器时,它无法打印消息?有人可以帮帮我吗?感谢。

-(void)main
{
    NSRunLoop *currentThreadRunLoop = [NSRunLoop currentRunLoop];
    [currentThreadRunLoop runMode:NSDefaultRunLoopMode beforeDate:    [NSDate distantFuture]];
    [NSTimer scheduledTimerWithTimeInterval:1.0 target:self      selector:@selector(handleTimerTask) userInfo:nil repeats:YES];
}

-(void) handleTimerTask
{
    NSLog(@"I am in timer task!");
}

@end

1 个答案:

答案 0 :(得分:0)

简单而基本的是,如果运行UIApplication的runloop,它将阻止所有操作并使UI无响应