Created
November 9, 2016 22:36
-
-
Save patzearfoss/8725433b25957a854f86e2992f1bfb5e to your computer and use it in GitHub Desktop.
Timer
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
class ThingManager { | |
var things: [String: Thing] = [:] | |
func addThing(_ thing: Thing) { | |
self.things[thing.address] = thing | |
} | |
func removeThingWith(address: String) { | |
self.things.removeValue(forKey: address) | |
} | |
fileprivate var thingWatchingQueue = DispatchQueue.main//DispatchQueue.global(qos: .utility) | |
fileprivate var thingWatchingTimers: [String: Timer] = [:] | |
func sawSomeThing(withAddress address: String) { | |
self.thingWatchingQueue.async { | |
// re-up the timer so we don't forget about that thing | |
if let timer = self.thingWatchingTimers[address] { | |
timer.invalidate() | |
} | |
self.thingWatchingTimers[address] = Timer.scheduledTimer(withTimeInterval: 5.0, repeats: false, block: { (timer) in | |
self.removeThingWith(address: address) | |
}) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment