Skip to content

Instantly share code, notes, and snippets.

@Wassmd
Created June 24, 2020 04:01
Show Gist options
  • Save Wassmd/e4d0cf099368695e0d3219633aeeeb14 to your computer and use it in GitHub Desktop.
Save Wassmd/e4d0cf099368695e0d3219633aeeeb14 to your computer and use it in GitHub Desktop.
Problem with Unowned in closure
import UIKit
class UnownedDemo {
func takeClosure(completion: @escaping () -> Void) {
DispatchQueue.main.asyncAfter(deadline: .now() + 5) {
completion()
}
}
}
class Test {
let unownedDemo = UnownedDemo()
func testClosure() {
unownedDemo.takeClosure { [unowned self] in
// unownedDemo.takeClosure { [weak self] in
self?.callHeMan()
}
}
private func callHeMan() {
print("Hello He Man")
}
}
class Test2 {
var test: Test? = Test()
func disposeTest() {
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
print("disposed...")
// self.test = nil
}
}
func call() {
print("Calling...")
test?.testClosure()
}
func callWithDispose() {
print("callWithDispose...")
test?.testClosure()
disposeTest()
}
}
let test2 = Test2()
//test2.call()
test2.callWithDispose()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment