1.时间不准的原因
NSTimer
是基于RunLoop
实现的, 当RunLoop
中有大量任务的情况下NSTimer
就会不准
解决办法:
使用GCD的定时器dispatch_source_t
(基于系统内核)
dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatchQueue);
dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, intervalInSeconds * NSEC_PER_SEC, leewayInSeconds * NSEC_PER_SEC);
dispatch_source_set_event_handler(timer, ^{
#code to be executed when timer fires
});
dispatch_resume(timer);
网友评论