GCD定时器
NSTimer依赖于RunLoop,如果RunLoop的任务过于繁重,可能会导致NSTimer不准时。
而GCD的定时器会更加准时。
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
_timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_queue_create("001", DISPATCH_QUEUE_CONCURRENT));
dispatch_source_set_timer(_timer, DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC, 1.0 * NSEC_PER_SEC);
dispatch_source_set_event_handler(_timer, ^{
[self test];
});
dispatch_resume(_timer);
}
- (void)viewWillDisappear:(BOOL)animated{
_timer = nil;
}
- (void)test{
NSLog(@"%s",__func__);
}
- (void)dealloc{
NSLog(@"%s",__func__);
}