In this example, will vm and targetVC get deinitialized? will it cause a memory leak?
loginModule.checkbox.checkboxAction = { [unowned self] in
let vm = HomeViewModel()
let targetVC = HomeViewController(viewModel: vm)
navigationController?.setViewControllers([targetVC], animated: true)
}
>Solution :
Neither vm and targetVC are captured, because they’re defined with the closure. They like any ol’ local variable.
-
vmwon’t be deinitialized, because the newHomeViewControllerkeeps a reference to it (I’m assuming it’s a strong references, because it wouldn’t make sense otherwise. -
targetVCprobably won’t get deintialized, becausenavigationControllerwill be holding a reference to it. There is an oddity here though, that thenavigationControlleris optional, so this set will only happen if it’s not nil. It being optional is weird, and probably improper. It should have been unwrapped earlier.