简体   繁体   English

如何在iOS上延迟块调用来避免保留周期

[英]How to avoid retain cycle with delaying block invocation on iOS

I am really stumped with this one. 我真的很难受这个。 I have been reading all the info I can get my hands on about how to properly handle variables inside blocks. 我一直在阅读有关如何正确处理块内变量的所有信息。

Note: I am not using arc. 注意:我没有使用arc。

So, say I have an animation that looks like this: 所以,假设我有一个看起来像这样的动画:

[UIView animateWithDuration:.5 animations:^{
    textCard.alpha = 1.f;
    self.dotView.alpha = 1.f;
    self.subtitles.alpha = 0;
}completion:^(BOOL finished){
    [self.playerLayer removeFromSuperlayer];
    self.playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.vidPlayer];
    self.playerLayer.frame = self.vidView.bounds;
    [self.vidView.layer insertSublayer:self.playerLayer atIndex:0];
    [self.subtitles removeFromSuperview];
    self.subtitles = [sub autorelease];

    [UIView animateWithDuration:.3 delay:1 options:UIViewAnimationOptionAllowUserInteraction animations:^{
        textCard.alpha = 0.f;
        sub.alpha = 1.f;
    }completion:^(BOOL finished){
        self.queuedVid = NO;
        if (self.shouldPlay == YES) {

            [self.vidPlayer play];
            [self setCurrentVideoSettings];
        }
        else{
            self.shouldPlay = YES;
        }

        [textCard removeFromSuperview];
        textCard = nil;
    }];
    }];

Now this code probably looks a little arbitrary but here is a very real world example of something I have to do. 现在这段代码可能看起来有点武断,但这是一个我必须做的事情的真实世界的例子。

Here are my two problems: 这是我的两个问题:

  1. If I leave this as is and the class (tries) to get deallocated during the invocation of this block, it will not release. 如果我保持原样,并且在调用此块期间类(尝试)被取消分配,则不会释放。

  2. If I change self to __block typeof (self) weakRef = self; 如果我把自己改为__block typeof (self) weakRef = self; then if the class gets deallocated before the second block can run, it crashes because it is trying to reference my deallocated self. 然后,如果在第二个块可以运行之前该类被释放,它会崩溃,因为它试图引用我的deallocated self。

I know I just must not understand how this works, but if anyone can shed some light, I would appreciate it. 我知道我一定不能理解这是如何运作的,但如果有人能说出一些亮点,我将不胜感激。

You shouldn't worry about retain cycles created by references to self in inline animation blocks. 您不必担心在内联动画块中通过引用self创建的保留周期。 The block will retain self when the animation begins, and release it when the animation block finishes executing. 动画开始时,块将保留self,并在动画块完成执行时释放它。

Imagine someone does something that invokes your animation. 想象一下有人做了一些调用动画的东西。 Then, before the animation completes, they pop that view controller off the navigation stack. 然后,在动画完成之前,他们将该视图控制器从导航堆栈中弹出。 The navigation controller releases your controller, but the animation block continues to retain it. 导航控制器释放您的控制器,但动画块继续保留它。 When the animation completes (off screen), the block will release what should be the last reference to your controller, and it will be deallocated. 当动画完成时(屏幕外),该块将释放应该是控制器的最后一个引用,并且它将被释放。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM