I'm implementing a simple ItemListing app where the ProductList viewController manages a table view which shows the results of a call to a REST service. The Detail viewController manages a view where I show more information about the item selected in the ProductListVC. I'm trying to apply MVVM pattern. I am using closures for the binding between view model and View controller. In the ProductList viewController, I create and initialize its viewModel this way:
class ProductListVC: UIViewController {
var viewModel: ProductViewModel = ProductViewModel()
var productTblView = UITableView()
override func viewDidLoad() {
super.viewDidLoad()
initViewModel()
}
func initViewModel() {
viewModel.reloadTableViewClosure = { [weak self] () in
DispatchQueue.main.async {
self?.productTblView.reloadData()
}
}
viewModel.fetchData()
}
}
My question is what is the need of using [weak self] in reloadTableViewClosure? How the strong reference happens in this case? It would be great if anybody can explain the usage of [weak self] or [unowned self] in this case. My confusion is, since the reloadTableViewClosure is owned by viewmodel how self maintains strong reference to closure?