简体   繁体   English

我们是否需要在ARC的UIAnimationBlocks内部使用__weak self?

[英]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.

相关问题 如果从Block调用的方法使用self,是否需要使用弱self指针? - Do I need to use a weak self pointer if a method called from a Block uses self? 在此关闭过程中,我需要[无主的自我]还是[弱的自我]? - Do I need [unowned self] or [weak self] in this closure? 如果我使用单一网络服务发出网络请求,是否需要使用[弱自我]? - Do I need to use [weak self] if I'm making a network request using a singleton networking service? 我们是否需要在 swift 闭包中为弱变量显式使用捕获列表? - Do we need to explicitly use capture list for weak variables in swift closure? 在_ ARC中使用__weak到局部变量 - use __weak to local variable in ARC 在ARC中将自己分配给__weak时应用程序崩溃了吗? - app crashing when assigning self to __weak in ARC? 在ARC中总是将自我的弱引用传递给块? - Always pass weak reference of self into block in ARC? 我们需要包含哪些库才能使用 CGPoint 和 arc4random()? - What libraries do we need to include to use CGPoint and arc4random()? 可选关闭是否总是 escaping? 我们应该在上面使用[weak self]还是[unowned self]? - Is optional closure always escaping? Should we use [weak self] or [unowned self] on it? 我需要在ARC中使用dealloc方法吗? - Do I need use dealloc method with ARC?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM