Skip to content

Commit fbfb249

Browse files
committed
Merge pull request #22 from Austinate/master
Removed NSTimerActor. Used CFRunLoopTimerCreateWithHandler instead
2 parents 0fbea81 + 7290d49 commit fbfb249

File tree

1 file changed

+10
-20
lines changed

1 file changed

+10
-20
lines changed

Sources/SwiftyTimer.swift

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,41 +24,31 @@
2424

2525
import Foundation
2626

27-
private class NSTimerActor {
28-
let block: () -> Void
29-
30-
init(_ block: () -> Void) {
31-
self.block = block
32-
}
33-
34-
@objc func fire() {
35-
block()
36-
}
37-
}
38-
3927
extension NSTimer {
4028
/// Create a timer that will call `block` once after the specified time.
4129
///
4230
/// - Note: The timer won't fire until it's scheduled on the run loop.
4331
/// Use `NSTimer.after` to create and schedule a timer in one step.
4432
/// - Note: The `new` class function is a workaround for a crashing bug when using convenience initializers (rdar://18720947)
45-
33+
4634
public class func new(after interval: NSTimeInterval, _ block: () -> Void) -> NSTimer {
47-
let actor = NSTimerActor(block)
48-
return self.init(timeInterval: interval, target: actor, selector: "fire", userInfo: nil, repeats: false)
35+
return CFRunLoopTimerCreateWithHandler(kCFAllocatorDefault, CFAbsoluteTimeGetCurrent() + interval, 0, 0, 0) { _ in
36+
block()
37+
}
4938
}
50-
5139
/// Create a timer that will call `block` repeatedly in specified time intervals.
5240
///
5341
/// - Note: The timer won't fire until it's scheduled on the run loop.
5442
/// Use `NSTimer.after` to create and schedule a timer in one step.
5543
/// - Note: The `new` class function is a workaround for a crashing bug when using convenience initializers (rdar://18720947)
56-
44+
5745
public class func new(every interval: NSTimeInterval, _ block: () -> Void) -> NSTimer {
58-
let actor = NSTimerActor(block)
59-
return self.init(timeInterval: interval, target: actor, selector: "fire", userInfo: nil, repeats: true)
46+
return CFRunLoopTimerCreateWithHandler(kCFAllocatorDefault, CFAbsoluteTimeGetCurrent() + interval, interval, 0, 0) { _ in
47+
block()
48+
}
6049
}
61-
50+
51+
6252
/// Create and schedule a timer that will call `block` once after the specified time.
6353

6454
public class func after(interval: NSTimeInterval, _ block: () -> Void) -> NSTimer {

0 commit comments

Comments
 (0)