GCD定时器

大家平时开发中使用最多的定时器应该是NSTimer了,但是,NSTimer同时也存在一些弊端:比如,有时候你要把它添加到不同的runloop model上才能保证它正常执行,又或者使用不当导致拥有它的对象无法释放,又或者NSTimer本身的机制导致它并不是很精确。

下面就介绍一下GCD定时器的实现:
GCD定时器其实是一种特殊的分派源,它是基于分派队列的,而NSTimer是基于运行循环的,所以,尤其是在多线程中,GCD定时器要比NSTimer好用的多。另外,GCD定时器使用dispatch_block_t,而不是方法选择器。

@interface GCDTimer : NSObject
+(GCDTimer *)repeatingTimerWithTimeInterval:(NSTimeInterval)seconds block:(dispatch_block_t)block;
@end
@interface GCDTimer()
@property (nonatomic, strong) dispatch_block_t block;
@property (nonatomic, strong) dispatch_source_t source;
@end

@implementation GCDTimer
+(GCDTimer *)repeatingTimerWithTimeInterval:(NSTimeInterval)seconds block:(dispatch_block_t)block {
    GCDTimer *timer = [[self alloc] init];
    timer.block = block;
    timer.source = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_main_queue()); // 首先,创建一个定时器分派源并绑定到主分派队列上,这使得定时器总是在主线程上触发
    uint64_t nsec = (uint64_t)(seconds * NSEC_PER_SEC);
    dispatch_source_set_timer(timer.source, dispatch_time(DISPATCH_TIME_NOW, nsec), nsec, 0);//设置定时器
    dispatch_source_set_event_handler(timer.source, block);//设置事件处理程序
    dispatch_resume(timer.source);//打开定时器(分派源通常都是需要配置的,所以它们创建的时候处于暂停状态,只有resume之后才会开始发送事件)
    return timer;
}

-(void)invalidate {
    if (self.source) {
        dispatch_source_cancel(self.source);
        self.source = nil;
    }
    self.block = nil;
}

-(void)dealloc {
    [self invalidate];//销毁时将定时器设置为无效
}
@end

GCD定时器在各种runloop model下都是可以执行的,因为它并不依赖与此,而NStimer,比如UIScrollView滚动的时候就需要添加到特定的model上才能执行。

如果要后台执行定时器,只要添加一下设置即可:

- (void)applicationDidEnterBackground:(UIApplication *)application {
    UIApplication*   app = [UIApplication sharedApplication];
    __block  UIBackgroundTaskIdentifier bgTask;
    bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
        dispatch_async(dispatch_get_main_queue(), ^{
            if (bgTask != UIBackgroundTaskInvalid) {
                bgTask = UIBackgroundTaskInvalid;
            }
        });
    }];
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        dispatch_async(dispatch_get_main_queue(), ^{
            if (bgTask != UIBackgroundTaskInvalid) {
                bgTask = UIBackgroundTaskInvalid;
            }
        });
    });
}









下面是swift 3.0的写法:

class GCDTimer {
    var block: () -> Void
    var source: DispatchSourceTimer
    
    init(block: @escaping ()->Void, source: DispatchSourceTimer) {
        self.block = block
        self.source = source
    }

    
    class func repeatingTimer(timeInterval seconds: Double, block: @escaping () -> Void) -> GCDTimer {
        let source = DispatchSource.makeTimerSource(flags: [], queue: DispatchQueue.main)
        let timer = GCDTimer(block: block, source: source)
        timer.source.scheduleRepeating(deadline: DispatchTime.now(), interval: seconds)
        timer.source.setEventHandler(handler: block)
        timer.source.resume()
        return timer
    }
    
    deinit {
        self.source.cancel()
    }
}

后台执行:

    func applicationDidEnterBackground(_ application: UIApplication) {
        
        let app = UIApplication.shared
        var bgTask: UIBackgroundTaskIdentifier!
        bgTask = app.beginBackgroundTask(expirationHandler: { 
            DispatchQueue.main.async {
                if bgTask != UIBackgroundTaskInvalid {
                    bgTask = UIBackgroundTaskInvalid
                }
            }
        })

        DispatchQueue.global().async {
            DispatchQueue.main.async {
                if bgTask != UIBackgroundTaskInvalid {
                    bgTask = UIBackgroundTaskInvalid
                }
            }
        }
    }

祝大家玩的愉快!

你可能感兴趣的:(GCD定时器)