iOS NSTimer 和 GCD 定时器及封装
iOS NSTimer 不准时
NSTimer 内部是有 runloop 的,我们的runloop是一直跑圈的,我们的runloop跑一圈回来的时候会看一下当前用时,比如,跑一圈的时候为0.2s,发现没到1s,继续跑圈,0.3,0.2,下一次跑圈,runloop里面有个耗时操作,用了0.5s,这时候,回来已经超过1s了,一共1.2s才会去执行timer的任务,所以NSTimer是不准的。
GCD 定时器
// 创建定时器
dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_main_queue() );
// 需要强引用保持定时器对象
_timer = timer;
// 设置时间,从什么时候开始,间隔多少,下面相当于2s后开始,每隔一秒一次
dispatch_source_set_timer(timer, dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC), 1 * NSEC_PER_SEC, 0);
// 回调
dispatch_source_set_event_handler(timer, ^{
NSLog(@"1111");
});
// 这个方法可以设置函数回调
// dispatch_source_set_event_handler_f(timer, gcdPrint);
dispatch_resume(timer);
GCD 和 runloop 没有关系,所以runloop的那些mode,比如在滑动tableView的时候,timer失效的问题,在GCDtimer是无效的,因为GCD和runloop没有关系,NSTimer在子线程开启的时候,需要添加到runloop里面,并设置mode,而我们的GCD不需要。
封装 GCD
+ (NSString *)timerWithStartTime:(NSTimeInterval)start interval:(NSTimeInterval)interval repeats:(BOOL)repeats mainQueue:(BOOL)async completion:(void (^)(void))completion;
+ (NSString *)timerWithTarget:(id)target selector:(SEL)selector StartTime:(NSTimeInterval)start interval:(NSTimeInterval)interval repeats:(BOOL)repeats mainQueue:(BOOL)async;
+ (void)cancel:(NSString *)timerID;
#import "SCXTimer.h"
@implementation SCXTimer
static int i = 0;
// 创建保存timer的容器
static NSMutableDictionary *timers;
dispatch_semaphore_t sem;
+(void)initialize{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
timers = [NSMutableDictionary dictionary];
sem = dispatch_semaphore_create(1);
});
}
+(NSString *)timerWithTarget:(id)target selector:(SEL)selector StartTime:(NSTimeInterval)start interval:(NSTimeInterval)interval repeats:(BOOL)repeats mainQueue:(BOOL)async{
if (!target || !selector) {
return nil;
}
return [self timerWithStartTime:start interval:interval repeats:repeats mainQueue:async completion:^{
if ([target respondsToSelector:selector]) {
[target performSelector:selector];
}
}];
}
+(NSString *)timerWithStartTime:(NSTimeInterval)start interval:(NSTimeInterval)interval repeats:(BOOL)repeats mainQueue:(BOOL)async completion:(void (^)(void))completion{
if (!completion || start < 0 || interval <= 0) {
return nil;
}
// 创建定时器
dispatch_queue_t queue = !async ? dispatch_queue_create("gcd.timer.queue", NULL) : dispatch_get_main_queue();
dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue );
// 设置时间,从什么时候开始,间隔多少,下面相当于2s后开始,每隔一秒一次
dispatch_source_set_timer(timer, dispatch_time(DISPATCH_TIME_NOW, start * NSEC_PER_SEC), interval * NSEC_PER_SEC, 0);
dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
NSString *timerId = [NSString stringWithFormat:@"%d",i++];
timers[timerId]=timer;
dispatch_semaphore_signal(sem);
// 回调
dispatch_source_set_event_handler(timer, ^{
if (completion) {
completion();
}
// 不重复执行就取消timer
if (!repeats) {
[self cancel:timerId];
}
});
dispatch_resume(timer);
return timerId;
}
+ (void)cancel:(NSString *)timerID{
if (!timerID || timerID.length <= 0) {
return;
}
dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
dispatch_source_t timer = timers[timerID];
if (timer) {
dispatch_source_cancel(timer);
[timers removeObjectForKey:timerID];
}
dispatch_semaphore_signal(sem);
}
@end
使用
[SCXTimer timerWithStartTime:5 interval:1 repeats:NO mainQueue:NO completion:^{
NSLog(@"12333333:%@",[NSThread currentThread]);
}];
网友评论