[英]Do we need to use __weak self inside UIAnimationBlocks in ARC?
Do we need to use __weak self inside UIAnimation Blocks as given below? 我们是否需要在UIAnimation块内使用__weak self,如下所示? Whether it will create retain cycle issue if we are not specifying self as weak? 如果我们不将self指定为弱,是否会产生保留周期问题?
[UIView animateWithDuration:animationDuration
delay:0
options:UIViewAnimationCurveEaseInOut
animations:^{
[self doSomething];
} completion:^(BOOL finished) {
if (finished) {
[self doSomething];
}
}];
I am also confused in the following scenario. 在以下情况下,我也感到困惑。 Any thoughts on this? 有什么想法吗? please share your comments. 请分享您的评论。
[self.navController dismissViewControllerAnimated:animated
completion:^{
[self doSomething];
}];
Should we use weak self here? 我们应该在这里使用弱者吗?
This is not a retain cycle. 这不是保留周期。 A retain cycle would be 保留周期为
self -> block -> self
In this case we have 在这种情况下,我们有
animation framework -> block
block -> self
where the first retain is only temporary - the block gets released when the animation ends. 其中第一个保留只是临时的-动画结束时将释放该块。 Even if a retain cycle happens, it will be only temporary and it won't prevent object deallocation. 即使发生保留周期,也只是暂时的,不会阻止对象的重新分配。
You need to use __weak
when retain cycle is possible. 如果可以进行保留循环,则需要使用__weak
。 This is not that case because animations block is not retained by self. 并不是这种情况,因为动画块不是自己保留的。
Another situation to use __weak
is a prolonged action that will call our block after completion and self
can be deallocated during this action. 使用__weak
另一种情况是长时间的操作,该操作将在完成后调用我们的块,并且可以在此操作期间将self
释放。 For example, some network request will call interface update for our view controller in completion block. 例如,某些网络请求将在完成模块中为我们的视图控制器调用接口更新。 User can exit our screen during request. 用户可以在请求期间退出我们的屏幕。 In this situation no need to retain self
with a block, it's better to use weak self. 在这种情况下,无需保留self
,最好使用弱自我。 But using animation blocks is not this situation too. 但是使用动画块也不是这种情况。
No, it won't create a retain cycle, because the block (closure) is not attached to self
. 不,不会创建保留周期,因为该块(关闭)未附加到self
。
For more information, please check out Apple's Automatic Reference Counting . 有关更多信息,请查阅Apple的自动参考计数 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.