Here is my method to fetch some data from the network:
func fetchProducts(parameters: [String: Any],
success: @escaping ([Product]) -> Void)
As you noticed, it has escaping closure. Here is how I call above method in ViewModel:
service.fetchProducts(parameters: params, success: { response in
self.isLoading?(false)
/// doing something with response
})
The question is should I capture self
weakly or strongly? Why? I think I can capture it strongly. Because, fetchProducts
is a function which has closure as a parameter. But, I might be wrong. But, from other perspective, I think it should be weak. Because, ViewModel has strong reference to service
, service
has strong reference to success
closure which has strong reference to self
(which is ViewModel). It creates retain cycle. But deinit
of ViewModel is called anyway, after ViewController
which owns ViewModel is deinitialized. It means that there was no retain cycle. Why?