今天突然试了一下timer在线程中启动,果然不行,看了一下之前调研过的文章,解决方案就是放在主线程或者放在runloop即可
- (void)viewDidLoad
{
[super viewDidLoad];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
dispatch_async(dispatch_get_main_queue(), ^{
NSTimer *time = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(test) userInfo:Nil repeats:YES];
});
// [[NSRunLoop currentRunLoop] addTimer:time forMode:NSDefaultRunLoopMode];
// [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:INTMAX_MAX]];
// [time fire];
});
}
- (void)test{
NSLog(@"11");
}