Won
Won

Reputation: 1226

UICollectionview.performBatchUpdates updates block does not need explicit use of self in capture list

When using collectionview.performBatchUpdates, the updates block does not need explicit self like below image.

enter image description here

The method's docs are here. https://developer.apple.com/documentation/uikit/uicollectionview/1618045-performbatchupdates

My guesss is that performBatchUpdates's updates block life cycle is garanteed shorter than collectionView. And the updates block called using withoutActuallyEscaping.

Is there anyone have clear answer?


The answer is simple. Because it's not a escaping closure

Upvotes: 0

Views: 314

Answers (1)

Paulw11
Paulw11

Reputation: 114974

The animate closure is escaping, while the performBatchUpdates closure is not.

A closure is said to escape a function when the closure is passed as an argument to the function, but is called after the function returns. When you declare a function that takes a closure as one of its parameters, you can write @escaping before the parameter’s type to indicate that the closure is allowed to escape

An escaping closure that refers to self needs special consideration if self refers to an instance of a class. Capturing self in an escaping closure makes it easy to accidentally create a strong reference cycle. For information about reference cycles, see Automatic Reference Counting.

Normally, a closure captures variables implicitly by using them in the body of the closure, but in this case you need to be explicit. If you want to capture self, write self explicitly when you use it, or include self in the closure’s capture list. Writing self explicitly lets you express your intent, and reminds you to confirm that there isn’t a reference cycle.

Upvotes: 2

Related Questions