Skip to content

Instantly share code, notes, and snippets.

@patzearfoss
Created November 9, 2016 22:36
Show Gist options
  • Save patzearfoss/8725433b25957a854f86e2992f1bfb5e to your computer and use it in GitHub Desktop.
Save patzearfoss/8725433b25957a854f86e2992f1bfb5e to your computer and use it in GitHub Desktop.
Timer
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