-
-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replacing NSTimers with dispatch timers
- Loading branch information
Paul Tsochantaris
authored and
Paul Tsochantaris
committed
Jan 31, 2017
1 parent
bef5bdb
commit 890fcc1
Showing
12 changed files
with
75 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,27 @@ | ||
|
||
#if os(iOS) | ||
import UIKit | ||
#endif | ||
import Foundation | ||
|
||
final class PopTimer { | ||
|
||
private var _popTimer: Timer? | ||
private let _timeInterval: TimeInterval | ||
private let _callback: () -> Void | ||
private var popTimer: Timer? | ||
private let timeInterval: TimeInterval | ||
private let callback: Completion | ||
|
||
func push() { | ||
_popTimer?.invalidate() | ||
_popTimer = Timer.scheduledTimer(timeInterval: _timeInterval, target: self, selector: #selector(popped), userInfo: nil, repeats: false) | ||
} | ||
|
||
@objc | ||
func popped() { | ||
invalidate() | ||
_callback() | ||
popTimer?.invalidate() | ||
popTimer = Timer(repeats: false, interval: timeInterval) { [weak self] in | ||
self?.invalidate() | ||
self?.callback() | ||
} | ||
} | ||
|
||
func invalidate() { | ||
_popTimer?.invalidate() | ||
_popTimer = nil | ||
popTimer?.invalidate() | ||
popTimer = nil | ||
} | ||
|
||
init(timeInterval: TimeInterval, callback: @escaping Completion) { | ||
_timeInterval = timeInterval | ||
_callback = callback | ||
self.timeInterval = timeInterval | ||
self.callback = callback | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
|
||
import Foundation | ||
|
||
final class Timer { | ||
|
||
private let timer = DispatchSource.makeTimerSource(flags: [], queue: DispatchQueue.main) | ||
private let completion: Completion | ||
|
||
init(repeats: Bool, interval: TimeInterval, block: @escaping Completion) { | ||
completion = block | ||
|
||
if repeats { | ||
timer.scheduleRepeating(deadline: .now() + interval, interval: interval) | ||
} else { | ||
timer.scheduleOneshot(deadline: .now() + interval) | ||
} | ||
timer.setEventHandler(handler: completion) | ||
timer.resume() | ||
} | ||
|
||
func invalidate() { | ||
timer.cancel() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters