iOS定时器 NSTimer、CADisplayLink、GCD

前言

在最近的项目开发过程中,遇到了需要运用定时器的需求,在以前的项目中,也没用过定时器。最近做的一个项目很有几个地方用到了定时器,短信验证码倒计时、倒计时取消订单,定时网络请求。在查阅相关资料过后,也解决了项目中的需求。下面介绍下几种定时器的用法吧! #CADisplayLink CADisplayLink是一个能让我们以和屏幕刷新率相同的频率将内容画到屏幕上的定时器。我们在应用中创建一个新的 CADisplayLink 对象,把它添加到一个runloop中,并给它提供一个 target 和selector 在屏幕刷新的时候调用。

  • 创建
    /**  < 创建CADisplayLink >  */
    self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(printSeconds)];
    /**  < 设置每秒刷新一次 The default value is 60 >  */
    self.displayLink.preferredFramesPerSecond = 1;
    /**  < 注册到RunLoop中 NSDefaultRunLoopMode >  */
    [self.displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    /**  < 暂停定时器 Initial state is
      false >  */
    self.displayLink.paused = YES;
复制代码
  • 暂停 self.displayLink.paused = YES;
  • 开始 self.displayLink.paused = NO;
  • 销毁
     /**  < 销毁定时器 >  */
    [self.displayLink invalidate];
    self.displayLink = nil;
复制代码

最后 CADisplayLink相比NSTimer来说,精度要高的多,如果有想更深入的了解可以查看官方文档或相关博客(CADisplayLink)介绍的很详细。

NSTimer

在最近开发的项目中,用NSTimer实现了定时请求接口,后台服务器要对前端App状态进行检测,要求App在用户登录的状态下,每30s请求一次接口,以便后台更好的处理先关业务逻辑。在App中只处理了在前台的情况,App处于后台,NSTimer会处于挂起状态,不执行,除非申请后台执行代码的权限。下面介绍NSTimer的基本使用方法。

  • NSTimer创建 常用的有两种创建方法,区别是前者需手动加入RunLoop,后者自动将 timer加入到当前线程的RunLoop中
//
+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(nullable id)userInfo repeats:(BOOL)yesOrNo;
//
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(nullable id)userInfo repeats:(BOOL)yesOrNo; 
复制代码

Block方法,iOS 10新出的API,使用这个两个方法,要考虑到兼容性问题

+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)interval repeats:(BOOL)repeats block:(void (^)(NSTimer *timer))block API_AVAILABLE(macosx(10.12), ios(10.0), watchos(3.0), tvos(10.0));
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)interval repeats:(BOOL)repeats block:(void (^)(NSTimer *timer))block API_AVAILABLE(macosx(10.12), ios(10.0), watchos(3.0), tvos(10.0));
复制代码

其它创建方法请参考头文件方法声明

timerWithTimeInterval

  1. 创建
self.timer = [NSTimer timerWithTimeInterval:1.f target:self selector:@selector(printSeconds) userInfo:nil repeats:YES];
复制代码
  • TimeInterval:执行之前等待的时间。比如设置成1.0,就代表1秒后执行方法
  • target:需要执行方法的对象
  • selector:需要执行的方法
  • repeats:是否需要循环
  1. 添加到Runloop(注意:上面这种方法创建的定时器必须添加到Runloop,否则定时器不会执行)
     /**
     < Default mode(NSDefaultRunLoopMode)
     默认模式中几乎包含了所有输入源(NSConnection除外),一般情况下应使用此模式。
     
     Connection mode(NSConnectionReplyMode)
     处理NSConnection对象相关事件,系统内部使用,用户基本不会使用。
     Modal mode(NSModalPanelRunLoopMode)
     处理modal panels事件。
     
     Event tracking mode(UITrackingRunLoopMode)
     在拖动loop或其他user interface tracking loops时处于此种模式下,在此模式下会限制输入事件的处理。例如,当手指按住UITableView拖动时就会处于此模式。
     
     Common mode(NSRunLoopCommonModes)
     这是一个伪模式,其为一组run loop mode的集合,将输入源加入此模式意味着在Common Modes中包含的所有模式下都可以处理。在Cocoa应用程序中,默认情况下Common Modes包含default modes,modal modes,event Tracking modes.可使用CFRunLoopAddCommonMode方法想Common Modes中添加自定义modes。
     >  */
[[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSDefaultRunLoopMode];
复制代码

scheduledTimerWithTimeInterval

/**  < 第二种创建方法 自动加入当前线程的RunLoop中,如果想让定时器不受滚动视图影响 应设置Mode为:NSRunLoopCommonModes  >  */
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.f target:self selector:@selector(printSeconds) userInfo:nil repeats:YES];
//    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
复制代码
  • 暂停、开始定时器
    /**  < 开始定时器 >  */
    if (self.timer.isValid) {
        self.timer.fireDate = [NSDate date];
    }

    /**  < 暂停定时器 >  */
    if (self.timer.isValid) {
        self.timer.fireDate = [NSDate distantFuture];
    }
复制代码
  • 销毁定时器
    [self.timer invalidate];
    self.timer = nil;
复制代码

GCD定时器

在项目中,短信验证码倒计时,和订单定时取消,都用到了GCD定时器,GCD定时器相对来说更为精准,但创建起来稍微麻烦点,下面就介绍基本用法吧!

  • 创建
//获取队列,这里获取全局队列(tips:可以单独创建一个队列跑定时器)
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//创建定时器(dispatch_source_t本质还是个OC对象)
self.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);

//start参数控制计时器第一次触发的时刻,延迟0s
dispatch_time_t start = dispatch_time(DISPATCH_TIME_NOW, 0 * NSEC_PER_SEC);
//    dispatch_time_t start = dispatch_walltime(NULL, 0);

//每隔1s执行一次
uint64_t interval = (uint64_t)(1.0 * NSEC_PER_SEC);
dispatch_source_set_timer(self.timer, start, interval, 0);
dispatch_source_set_event_handler(self.timer, ^{
    //要执行的任务
});
//开始执行定时器
dispatch_resume(self.timer);
复制代码
  • 开始定时器
dispatch_resume(self.timer);
复制代码
  • 暂停定时器
dispatch_suspend(self.timer);
复制代码
  • 取消定时器
dispatch_cancel(self.timer);
self.timer = nil;
复制代码

总结

公司的项目刚刚上线,稍微可以松动一下,自己花时间整理了一下定时器的实现方式,除了CADisplayLink,后两种定时器在项目中都用到了。使用的时候要注意定时器和Runloop之间的联系、以及定时器的销毁问题。我也是查阅了相关资料和结合项目中的实际应用整理出来的,如果有写的不对的地方,欢迎大家批评指正。希望文章能给需要的人有所帮助。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值