mirror of
https://github.com/wnagrodzki/iOSProgrammingGuidelines.git
synced 2025-05-03 17:41:33 +02:00
Added "Extending object's lifetime" section
This commit is contained in:
parent
09d5a5e51b
commit
3bf12d0471
1 changed files with 15 additions and 0 deletions
15
README.md
15
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.
|
||||
|
|
Loading…
Add table
Reference in a new issue