From 7290d49b8f3b0ad1281ade689fa24ca650f2da8e Mon Sep 17 00:00:00 2001 From: Ostap Taran Date: Mon, 29 Feb 2016 22:39:01 +0200 Subject: [PATCH] Removed NSTimerActor. Used CFRunLoopTimerCreateWithHandler instead --- Sources/SwiftyTimer.swift | 30 ++++++++++-------------------- 1 file changed, 10 insertions(+), 20 deletions(-) diff --git a/Sources/SwiftyTimer.swift b/Sources/SwiftyTimer.swift index 5a58c43..c76605d 100644 --- a/Sources/SwiftyTimer.swift +++ b/Sources/SwiftyTimer.swift @@ -24,41 +24,31 @@ import Foundation -private class NSTimerActor { - let block: () -> Void - - init(_ block: () -> Void) { - self.block = block - } - - @objc func fire() { - block() - } -} - extension NSTimer { /// Create a timer that will call `block` once after the specified time. /// /// - Note: The timer won't fire until it's scheduled on the run loop. /// Use `NSTimer.after` to create and schedule a timer in one step. /// - Note: The `new` class function is a workaround for a crashing bug when using convenience initializers (rdar://18720947) - + public class func new(after interval: NSTimeInterval, _ block: () -> Void) -> NSTimer { - let actor = NSTimerActor(block) - return self.init(timeInterval: interval, target: actor, selector: "fire", userInfo: nil, repeats: false) + return CFRunLoopTimerCreateWithHandler(kCFAllocatorDefault, CFAbsoluteTimeGetCurrent() + interval, 0, 0, 0) { _ in + block() + } } - /// Create a timer that will call `block` repeatedly in specified time intervals. /// /// - Note: The timer won't fire until it's scheduled on the run loop. /// Use `NSTimer.after` to create and schedule a timer in one step. /// - Note: The `new` class function is a workaround for a crashing bug when using convenience initializers (rdar://18720947) - + public class func new(every interval: NSTimeInterval, _ block: () -> Void) -> NSTimer { - let actor = NSTimerActor(block) - return self.init(timeInterval: interval, target: actor, selector: "fire", userInfo: nil, repeats: true) + return CFRunLoopTimerCreateWithHandler(kCFAllocatorDefault, CFAbsoluteTimeGetCurrent() + interval, interval, 0, 0) { _ in + block() + } } - + + /// Create and schedule a timer that will call `block` once after the specified time. public class func after(interval: NSTimeInterval, _ block: () -> Void) -> NSTimer {