iOS定时器任务

945 阅读5分钟

有3种方式:CADisplayLink 、NSTimer、GCD

CADisplayLink

特点:屏幕刷新时触发一次,会重复调用指定的方法。 CADisplayLink是一个能让我们以和屏幕刷新率同步的频率,将特定的内容画到屏幕上的定时器类。通常情况下,按照iOS设备屏幕的刷新率60次/秒。

CADisplayLink以特定模式注册到runloop后,每当屏幕显示内容刷新结束时,runloop就会向CADisplayLink指定的target发送一次指定的selector消息,即调用方法。

  • 创建
self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(handleDisplayLink:)];    
[self.displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
//当把CADisplayLink对象add到runloop中后,selector就能被周期性调用
  • 属性
`frameInterval`:
设置间隔多少帧`(NSInteger)`调用一次`selector`方法。默认是1,即每帧都调用一次。

`duration `:
表示两次屏幕刷新之间的时间间隔`(CFTimeInterval)`。只读属性。
注意:该属性在`target``selector`被首次调用以后才会被赋值。
`selector`的调用间隔时间t 计算方式是:`t = duration × frameInterval`
  • 停止及释放
[self.displayLink invalidate];  
 self.displayLink = nil;
//  CADisplayLink对象就会从runloop中移除,selector调用也随即停止
  • 优缺点 iOS设备的屏幕刷新频率是固定的,CADisplayLink在正常情况下会在每次刷新结束都被调用,精确度相当高。 1、但如果调用的定时方法比较耗时,超过了屏幕刷新周期,就会导致跳过若干次回调机会。 2、如果CPU过于繁忙,无法保证屏幕60次/秒的刷新率,就会导致跳过若干次调用回调机会。跳过次数取决CPU的忙碌程度。

  • 应用场景 从原理上可以看出,CADisplayLink适合做界面的不停重绘。比如,视频播放的时候需要不停地获取下一帧用于界面渲染。

NSTimer

  • 创建 计时器一定要加入RunLoop中,并且选好mode才能运行。
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(dosomething:) userInfo:nil repeats:NO];
// TimeInterval:执行之前的等待时间
// repeats : 是否需要循环

上面的创建方法创建一个计时器,并自动加入到MainRunloopNSDefaultRunLoopMode中,可以直接使用。 方法scheduledTimerWithTimeInterval 在哪个线程创建,就会被加入哪个线程的RunLoop中就运行在哪个线程。 自己创建的Timer,加入到哪个线程的RunLoop中就运行在哪个线程。

创建后,target对象的计数器会加1,直到执行完毕,会自动减1,自动释放。

  • 缺点:

1、存在延迟问题 不管是一次性的还是周期性,timer的实际触发时间,都会与所加入的RunLoop、RunLoop Mode有关。

如果所加入的RunLoop正在执行一个连续性的任务,timer就会被延时触发。对于重复性的timer,如果延迟超过了一个周期,则会在延时结束后立刻执行,并按照之前指定的周期继续执行。

2、页面滑动时,定时器不工作

主线程的RunLoop里有两个预置的模式: kCFRunLoopDefaultMode、UITrackingRunLoopMode都是"Common"属性。 前者是App平时所处的状态,后者是追踪滑动的状态。

当创建一个 Timer 并加到 DefaultMode 时,Timer 会得到重复回调。但如果出现页面的滑动,RunLoop 会将模式切换为 TrackingRunLoopMode(为了不影响滑动操作)但这时 Timer 就不会被回调。

解决: 滑动和定时器调用互不影响,将这个 Timer 加入模式为NSRunLoopCommonModes

// 创建
NSTimer *timer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
// 添加到runloop
[[NSRunLoop mainRunLoop] addTimer:timer forMode: NSRunLoopCommonModes];

获取runloop的方法 [NSRunLoop mainRunLoop] 这个适用于主线程中 [NSRunLoop currentRunLoop]主线程/子线程 都适用

  • 释放 如果是定时器任务循环执行的话,就必须手动关闭释放!
[timer invalidate];
timer = nil;
释放Timer的注意点:

不能在dealloc中释放定时器。因为计时器的repeatsYES时,计时器会强持有self,导致dealloc永远不会被调用,这个类就永远无法被释放。

解决:可以在viewDidDisappear中释放,这样当类需要被回收时,就可以正常进入dealloc中了。

暂停Timer

需求:先暂停,然后再某种情况下再次开启。主要是为了防止它在后台运行,暂用CPU。 比如,在页面消失的时候关闭 ,然后等页面再次打开 ,又开启定时器。

//关闭定时器  
[myTimer setFireDate:[NSDate distantFuture]];  

//开启定时器  
[myTimer setFireDate:[NSDate distantPast]]; 

GCD

执行一次

dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 2.0 * NSEC_PER_SEC); 
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ 
    //执行事件
});

重复

NSTimeInterval period = 1.0; // 时间间隔
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
dispatch_source_set_timer(_timer, dispatch_walltime(NULL, 0), period * NSEC_PER_SEC, 0);  
dispatch_source_set_event_handler(_timer, ^{
    //在这里执行事件
});
dispatch_resume(_timer);  // 启动

取消定时器: dispatch_cancel( _timer);

定时器的调用,放在主线程中最优! 在gcd dispatch_async中执行可能会无效!

循环引用问题

1、CADisplayLink、NSTimer会对target产生强引用,如果target又对它们强引用,就会引发循环引用。 2、runloop 会对 CADisplayLink、NSTimer产生强引用。

解决:使用weakSelf,或NSProxy(代理对象)

  • weakSelf
// 内部使用 WeakSelf, 并在视图消失前, 关闭定时器
__weak __typeof(self) weakSelf = self;
NSTimer * timer = [NSTimer timerWithTimeInterval:1 repeats:YES block:^(NSTimer * _Nonnull timer) {
    NSLog(@"timer");
}];
self.timer = timer;
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
  • NSProxy
.h
// 解决循环引用问题
@interface MyProxy : NSProxy
- (instancetype)initWithObjc:(id)objc;
+ (instancetype)proxyWithObjc:(id)objc;


.m
@interface MyProxy()
@property(nonatomic,weak) id objc;
@end

@implementation MyProxy
- (instancetype)initWithObjc:(id)objc{
    self.objc = objc;
    return self;
}
+ (instancetype)proxyWithObjc:(id)objc{
    return [[self alloc] initWithObjc:objc];
}
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector {
    return [self.objc methodSignatureForSelector:aSelector];
}
- (void)forwardInvocation:(NSInvocation *)invocation {
    if ([self.objc respondsToSelector:invocation.selector]) {
        [invocation invokeWithTarget:self.objc];
    }
}

用代理对象引用target。

NSTimer * timer = [NSTimer timerWithTimeInterval:1
                                          target:[TimerProxy proxyWithTarget:self]
                                        selector:@selector(test1)
                                        userInfo:nil
                                         repeats:YES];
self.timer = timer;
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];