ARC,块中的 ivars 和通过捕获的自我的参考周期 [英] ARC, ivars in Blocks and Reference Cycles via Captured Self

查看:19
本文介绍了ARC,块中的 ivars 和通过捕获的自我的参考周期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在纯 iOS5/ARC 环境中工作,所以我可以根据需要使用 __weak 引用.在许多情况下,我确实在块中引用了 ivars,最值得注意的是,移动视图的动画块,例如,我的视图控制器类的属性.

I’m working in a pure iOS5/ARC environment, so I can use __weak references as needed. I do reference ivars in a block in many situations, most notably, animation blocks that move views around, which are properties of say, my view controller class.

在一个块中最简单的 ivars 使用中,我是在创建一个引用循环吗?我是否需要使用 __weak self/strong self 技术每次我编写一个操作包含对象的实例变量的块?

In the most trivial use of ivars in a block, am I creating a reference cycle? Do I need to use the __weak self / strong self technique everytime I write a block that manipulates instance variables of the containing object?

我一直在重新观看 2011 年 WWDC 会议 #322(Objective-C 深度改进),以了解从时间索引 25:03 开始​​的 3 分钟片段通过捕获的自我进行参考循环"的细微差别.对我来说,这意味着块中任何 ivars 的使用都应该通过该部分中描述的弱自我/强自我设置来保护.

I’ve been re-watching the 2011 WWDC Session #322 (Objective-C Advancements in Depth) to understand the nuances regarding the 3 minute segment starting at time index 25:03 about "Reference Cycle Via Captured Self". To me, this implies any usage of ivars in a block should be safeguarded with the weak self / strong self setup as described in that segment.

以下视图控制器上的示例方法是我所做的典型动画.

The sample method below on a view controller, is typical of animations I do.

在openIris块中,像我一样引用ivars_topView"和_bottomView"是不是错了?

In the openIris block, is it wrong to reference ivars "_topView" and "_bottomView" as I have?

我是否应该始终在块之前设置对 self 的 __weak 引用,然后在块内对之前设置的弱引用设置强引用,然后通过块内的强引用访问 ivars?

Should I always setup a __weak reference to self before the block, then a strong reference inside the block to the weak reference just setup prior, and then access the ivars through that strong reference within my block?

从 WWDC 会议中,我了解到在块中引用 ivars 实际上是在创建对这些 ivars 所依赖的隐含自我的引用.

From the WWDC session, I understand that referencing ivars in a block is really creating a reference to the implied self that these ivars hang off of.

对我来说,这意味着真的没有任何简单或琐碎的情况可以在没有弱/强舞蹈以确保没有循环的情况下访问块中的 ivars 是正确的.或者我是否阅读了很多不适用于简单案例的极端案例,例如我的示例?

To me, this implies that there really isn’t any simple or trivial case where it is correct to access ivars in a block without the weak/strong dance to ensure no cycles. Or am I reading to much into a corner case that doesn’t apply to simple cases, such as my example?

- (void)openIrisAnimated:(BOOL)animated
{
    if (_isIrisOpened) {
        NSLog(@"Asked to open an already open iris.");
        return; // Bail
    }

    // Put the common work into a block.
    // Note: "_topView" and "_bottomView" are the backing ivars of 
    // properties "topView" and "bottomView"
    void (^openIris)() = ^{
        _topView.frame     = CGRectMake(....);        
        _bottomView.frame  = CGRectMake(....);
    };

    // Now do the actual opening of the iris, whether animated or not:
    if (animated) {
        [UIView animateWithDuration:0.70f 
                         animations:^{
                             openIris();
                         }];
    }
    else {
        openIris();
    }

    _irisOpened = YES; // Because we have now just opened it
}

以下是我使用 Session #322 的指导重写 openIris 块的方法,但我只是想知道我的所有类似块是否都需要这种弱/强参考舞蹈来确保正确性和稳定性:

Here’s how I’d re-write the openIris block piece using the guidance from Session #322, but I’m just wondering if all my similar blocks require this weak/strong reference dance to ensure correctness and stability:

__weak MyClass *weakSelf = self;


void (^openIris)() = ^{
     MyClass *strongSelf = weakSelf;

     if (strongSelf) {        
        strongSelf.topView.frame     = CGRectMake(....);
        strongSelf.bottomView.frame  = CGRectMake(....);
     }
};

这真的有必要吗?

推荐答案

这里只有一个循环,如果 self 然后继续持有对块的引用(或自己拥有的东西).如果不是,你最好去,因为块的生命周期不是由它保留的 self 决定的.

There is only a cycle here if self then goes on to hold a reference to the block (or something owned by self). If not you're good to go as the lifetime of the block is not dictated by the self it retained.

因此,在您的特定示例中,您似乎很清楚.动画块不需要参与弱/强自舞.

So in your particular example, you seem to be in the clear. Animation blocks don't need to participate in the weak/strong self dance.

这篇关于ARC,块中的 ivars 和通过捕获的自我的参考周期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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