Block retain cycles in Swift?

Swift

Swift Problem Overview


Traditionally in Objc, we do weakSelf to prevent additional retain count for blocks.

How does swift internally manage retain cycles that occur in blocks for Objc?

Swift Solutions


Solution 1 - Swift

To prevent a block from holding a strong reference to an object, you must define a capture list for the block.

The closure expression syntax is defined as follows:

{ ( /*parameters*/ ) -> /*return type*/ in
    
    // statements
}

But this is extended later in the documentation to include a capture list. This effectively equates to the expression syntax being defined as follows:

{ [ /*reference type*/ /*object*/, ... ] ( /*parameters*/ ) -> /*return type*/ in
    
    // statements
}

...where /*reference type*/ can be either weak or unowned.

The capture list is the first thing to appear in the closure and it is optional. The syntax, as shown above is defined as one or more pairs of reference type followed by object; each pair is separated by a comma. For example:

[unowned self, weak otherObject]

Complete example:

var myClosure = {
    [unowned self] in
    print(self.description)
}

Note that an unowned reference is non-optional, so you don't need to unwrap it.

Hopefully that answers your question. You can read up more about ARC in Swift in the relevant section of the documentation.

You should pay particular attention to the difference between weak and unowned. It could be safer in your implementation to use weak, because using unowned assumes the object will never be nil. This may lead to your app crashing if the object has actually been deallocated before being used in your closure.

Using weak as the reference type, you should unwrap with ?, as follows:

var myClosure = {
    [weak self] in
    print(self?.description)
}

Solution 2 - Swift

The only thing that threw me off with capture lists was when to use weak vs unowned.

The book distilled it down to these rules:

If self could be nil in the closure use [weak self].

If self will never be nil in the closure use [unowned self].

See the section Weak and Unowned References in The Swift Programming Language book for a deeper explanation.

Solution 3 - Swift

As described above there are 2 possibilities to avoid retain cycles in Swift and these are weak and unowned as described below:

var sampleClosure = { [unowned self] in
    self.doSomething()
}

where the self never can be nil.

var sampleClosure = { [weak self] in
    self?.doSomething()
}

where self need to be unwrapped using ?. Here there is a important observation to do, if there are more instructions that use self and can be share the results etc, a possible correct way can be:

var sampleClosure = { [weak self] in
    if let this = self{
       this.doSomething()
       this.doOtherThings()
    }             
}

or

var sampleClosure = { [weak self] in
    guard let strongSelf = self else{
       return
    }
    strongSelf.doSomething()
    strongSelf.doOtherThings()             
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAdrianView Question on Stackoverflow
Solution 1 - SwiftSamView Answer on Stackoverflow
Solution 2 - SwiftTenaciousJayView Answer on Stackoverflow
Solution 3 - SwiftciccioskaView Answer on Stackoverflow