Should I be weak in UIView.animate when there is a lag and in view mode there is a batch update?

All in all I know that we don't need to make ourselves weak when using UIView.animate () because the block is not strongly supported, but is there an argument to use weak in the next bit of code because of the procrastination? Why would someone say what could be?

UIView.animate(withDuration: 0.1, animations: {
  self.performAction()
}

      

In the next example, why do we need to use weak self / don't need to use weak self ...?

collectionView.performBatchUpdates({
    self.collectionView.reloadData()
    ...
})

      

+3


source to share


1 answer


Background:

Blocks / Closures are nothing more than counting objects in heap memory. When you create a block and hold a strong lock / close reference, you have declared that the block's reference count is incremented by 1.

Obviously, this means that the block will not be released even after it has been executed from memory, until all classes that are tightly holding a reference to the block are holding tightly.

Now with that in mind, if you pass a strong self to block, because the variables used inside the block are maintained until the block completes its execution (Contextual capture being the main difference between function and blocks) itself is not will be until the block itself is released.

Now it's deadLock :). Your self contains a strong reference to the block object and a block object that contains a strong reference to the self. Both will now wait for each other to free themselves and ultimately never let go of each other.

Answer your question:



As you pointed out, if you are not tightly holding the reference to the UIView.animate block, there is no compelling reason for you to pass a weak self, as in the case of batch updating a collection.

In your case

collectionView.performBatchUpdates({
    self.collection.reloadData()
    ...
})

      

I believe the collection is a collectionView, if it is an IBOutlet you should notice that it was declared as a weak object. So your code seems to be more like

collectionView.performBatchUpdates({
    self.collection?.reloadData()
    ...
})

      

Hope it helps

+4


source







All Articles