如何通过延迟iOS上的块调用来避免保留周期 [英] How to avoid retain cycle with delaying block invocation on iOS

查看:58
本文介绍了如何通过延迟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.

注意:我没有使用arc.

Note: I am not using 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.

这是我的两个问题:

  1. 如果我保持原样,并且在调用此块的过程中要释放的类(重试),它将不会释放.

  1. If I leave this as is and the class (tries) to get deallocated during the invocation of this block, it will not release.

如果我将self更改为__block typeof (self) weakRef = self;,则如果该类在第二个块可以运行之前被释放,则它会崩溃,因为它正在尝试引用我的释放的self.

If I change self to __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.

我知道我只是不明白它是如何工作的,但是如果有人可以阐明一点,我将不胜感激.

I know I just must not understand how this works, but if anyone can shed some light, I would appreciate it.

推荐答案

您不必担心在嵌入式动画块中通过引用self创建的保留周期.动画开始时,该块将保留自身,并在动画块完成执行时将其释放.

You shouldn't worry about retain cycles created by references to self in inline animation blocks. The block will retain self when the animation begins, and release it when the animation block finishes executing.

想象一下某人做某事来调用您的动画.然后,在动画完成之前,他们将视图控制器从导航堆栈中弹出.导航控制器会释放您的控制器,但动画块会继续保留它.动画完成时(屏幕外),该块将释放对控制器的最后一个引用,并将其释放.

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.

这篇关于如何通过延迟iOS上的块调用来避免保留周期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆