ARC,通过捕获的自我在块和参考循环中的ivars [英] ARC, ivars in Blocks and Reference Cycles via Captured Self
问题描述
我在纯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?
我是否应始终在块之前设置__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屋!