Skip to content

Instantly share code, notes, and snippets.

@itsdamslife
Last active February 15, 2017 16:09
Show Gist options
  • Save itsdamslife/4978fadbfcdf12886b727aa0bb541b27 to your computer and use it in GitHub Desktop.
Save itsdamslife/4978fadbfcdf12886b727aa0bb541b27 to your computer and use it in GitHub Desktop.
How do I check if myVC (the parent who created closure) is still alive before calling?
class Example {
var callbackRef: Closure?
var timer: Timer?
var incrementer: Int = 1
func test(repeating: Bool, callback: @escaping Closure) {
callbackRef = callback
_ = Timer.scheduledTimer(withTimeInterval: 2.0, repeats: true, block: { [unowned self]
timer in
self.newTest(callback: callback)
})
}
func newTest(callback: Closure) {
callback(true, self.incrementer)
self.incrementer += 1
}
}
class TestExample {
let ex: Example = Example()
func doSomething(repeatIt: Bool, callback: @escaping Closure1) {
print("repeatIt")
ex.test(repeating: repeatIt, callback: {
repeating, value in
callback("Printing \(value)")
})
}
}
class MyViewController {
init() {
let test: TestExample = TestExample()
test.doSomething(repeatIt: true, callback: {
response in
print("---->> \(response)")
})
}
}
/*
let myVC = MyViewController()
// do something
myVC = nil // After release of myVC object closure crashes...
***** How do I check if myVC (the parent who created closure) is still alive? *****
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment