Skip to content

Instantly share code, notes, and snippets.

@kyungpyoda
Created August 24, 2022 18:48
Show Gist options
  • Save kyungpyoda/86c22b1f837003ab5c83dde51d8d90f6 to your computer and use it in GitHub Desktop.
Save kyungpyoda/86c22b1f837003ab5c83dde51d8d90f6 to your computer and use it in GitHub Desktop.
nested function의 객체 소유권 삽질
import Foundation
class Dummy {
private let name = "Dummy"
}
final class SomeClass {
private let dodoService = DoDoService()
private let dummy = Dummy()
init() {
print(aa, "initialized")
}
deinit {
print(self, "deinitialized")
}
func aa() {
func nested() {
printing()
}
// DoService.shared.doLater {
// nested()
// }
dodoService.dodoLater {
// nested()
self.printing()
}
}
func printing() {
print(dummy)
}
}
final class DoService {
static let shared = DoService()
private init() {}
func doLater(completion: @escaping () -> Void) {
DispatchQueue.global().asyncAfter(deadline: .now() + 2) {
completion()
}
}
}
final class DoDoService {
init() {}
func dodoLater(completion: @escaping () -> Void) {
DispatchQueue.global().asyncAfter(deadline: .now() + 2) {
completion()
completion()
}
}
}
final class Flow {
init() {
start()
}
func start() {
let t = SomeClass()
t.aa()
}
}
let flow = Flow()
RunLoop.main.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment