From 3bf12d047129b9ae2df9ad78232b3e57b4f19294 Mon Sep 17 00:00:00 2001 From: Wojciech Nagrodzki <278594+wnagrodzki@users.noreply.github.com> Date: Sun, 7 Oct 2018 21:44:54 +0200 Subject: [PATCH] Added "Extending object's lifetime" section --- README.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/README.md b/README.md index 6701931..692d93d 100644 --- a/README.md +++ b/README.md @@ -195,3 +195,18 @@ struct Email: RawRepresentable { func showUser(with: Email) { } ``` > This is to increase source code reliability by building it on data you can trust. + +### Extending object's lifetime + +If you are breaking reference cycle for an object bind it to a strong reference for the duration of the closure’s execution. + +```swift +operation.onComplete { [weak self] result in + guard let self = self else { return } + let model = self.updateModel(result) + self.updateUI(model) +} +``` + +> This is to avoid deallocation of weakly referenced object during closure execution. +> Use `strongSelf` instead of `self` for local variable name until [SR-6156]( https://bugs.swift.org/browse/SR-6156) is fixed.