Skip to content

Instantly share code, notes, and snippets.

@lacyrhoades
Last active November 9, 2016 23:04
Show Gist options
  • Save lacyrhoades/6b196b6cdb5ed310edcd0f3e8efd3129 to your computer and use it in GitHub Desktop.
Save lacyrhoades/6b196b6cdb5ed310edcd0f3e8efd3129 to your computer and use it in GitHub Desktop.
func debug() {
let manager = ThingManager()
let thing1 = Thing(name: "thing1")
manager.addThing(thing1)
manager.sawSomeThing(named: thing1.name)
print("Manager has this many things: ", manager.things.count)
Timer.scheduledTimer(withTimeInterval: 10.0, repeats: false, block: { (timer) in
// By now, the manager should have forgotten about the thing
print("Manager has this many things: ", manager.things.count)
})
}
struct Thing {
var name: String
}
class ThingManager {
var things: [String: Thing] = [:]
fileprivate var thingWatchingRunLoop = RunLoop()
fileprivate var thingWatchingQueue = DispatchQueue.global()
fileprivate var thingWatchingTimers: [String: Timer] = [:]
func addThing(_ thing: Thing) {
self.things[thing.name] = thing
}
func sawSomeThing(named name: String) {
self.thingWatchingQueue.async {
// re-up the timer so we don't forget about that thing
if let timer = self.thingWatchingTimers[name] {
timer.invalidate()
}
let timer = Timer(timeInterval: 5.0, target: self, selector: #selector(self.timerWentOff(_:)), userInfo: ["name":name], repeats: false)
self.thingWatchingRunLoop.add(timer, forMode: .commonModes)
self.thingWatchingTimers[name] = timer
}
}
@objc func timerWentOff(_ timer: Timer) {
let info = timer.userInfo as! [String: String]
let name = info["name"]
self.removeThing(named: name!)
}
func removeThing(named name: String) {
self.things.removeValue(forKey: name)func debug() {
let manager = ThingManager()
let thing1 = Thing(name: "thing1")
manager.addThing(thing1)
manager.sawSomeThing(named: thing1.name)
print("Manager has this many things: ", manager.things.count)
Timer.scheduledTimer(withTimeInterval: 10.0, repeats: false, block: { (timer) in
// By now, the manager should have forgotten about the thing
print("Manager has this many things: ", manager.things.count)
})
}
struct Thing {
var name: String
}
class ThingManager {
var things: [String: Thing] = [:]
fileprivate var thingWatchingRunLoop = RunLoop()
fileprivate var thingWatchingQueue = DispatchQueue.global()
fileprivate var thingWatchingTimers: [String: Timer] = [:]
func addThing(_ thing: Thing) {
self.things[thing.name] = thing
}
func sawSomeThing(named name: String) {
self.thingWatchingQueue.async {
// re-up the timer so we don't forget about that thing
if let timer = self.thingWatchingTimers[name] {
timer.invalidate()
}
let timer = Timer(timeInterval: 5.0, target: self, selector: #selector(self.timerWentOff(_:)), userInfo: ["name":name], repeats: false)
self.thingWatchingRunLoop.add(timer, forMode: .commonModes)
self.thingWatchingTimers[name] = timer
}
}
@objc func timerWentOff(_ timer: Timer) {
let info = timer.userInfo as! [String: String]
let name = info["name"]
self.removeThing(named: name!)
}
func removeThing(named name: String) {
self.things.removeValue(forKey: name)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment