Closed alspirichev closed 5 years ago
Why is the strong reference an issue? There's no retain cycle here.
The closure captures scrollView
, but scrollView
doesn't capture the closure (e.g. no cycle in dependencies, no problem for each of them to be individually released).
When using self
, specifically, the closure captures self and self captures the closure - which is where the cycle happens.
Did you spot any memory issues with using the existing capture groups?
Thanks for your feedback!
Fully agree with @freak4pc
Ok, but why then do we explicitly use the capture list? without explicitly specifying the type of reference in it. We can omit the capture list in these examples.
@alspirichev please read more docs about capture list. Reference in capture list is a copy of original reference. So, that's not a reference on self. That prevents reference cycle and memory leak. Feel free to ask if you have some more questions :)
You're in a closure. If you won't capture scrollView
, you'll have to reference self.scrollView
explicitly - which would create a retain cycle between self and the closure.
I think we should use a weak reference (not a strong) in our examples. By default, and when we use square brackets without explicitly specifying the type of reference, Swift uses a strong reference to external objects.