idS*_*tar 26 objective-c ios objective-c-blocks ios5 automatic-ref-counting
我在纯iOS5/ARC环境中工作,所以我可以根据需要使用__weak引用.我在很多情况下都会在块中引用ivars,最值得注意的是,移动视图的动画块,也就是我的视图控制器类的属性.
在块中最平凡地使用ivars,我是否创建了一个参考周期?每次我编写一个操作包含对象的实例变量的块时,是否需要使用__weak self/strong self技术?
我一直在重新观看2011年WWDC会议#322(Objective-C Advancements in Depth),以了解从时间索引25:03开始的关于"参考周期通过捕获的自我"的3分钟段的细微差别.对我来说,这意味着任何块中ivars的使用都应该通过该段中描述的弱自我/强自我设置来保护.
下面的视图控制器上的示例方法是我做的动画的典型.
在openIris块中,引用ivars"_topView"和"_bottomView"是不对的?
我应该总是在块之前设置一个__weak引用自身,然后在块内强引用刚刚设置的弱引用,然后通过我块中的强引用访问ivars吗?
从WWDC会话中,我理解在块中引用ivars实际上是在创建对这些ivars挂起的隐含自我的引用.
对我而言,这意味着没有任何简单或微不足道的情况,在没有弱/强舞的情况下访问块中的ivars是正确的,以确保没有循环.或者我是否正在阅读一个不适用于简单案例的角落案例,例如我的例子?
- (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
}
Run Code Online (Sandbox Code Playgroud)
以下是我使用Session#322的指导重新编写openIris块的方法,但我只是想知道我所有类似的块是否需要这种弱/强参考舞以确保正确性和稳定性:
__weak MyClass *weakSelf = self;
void (^openIris)() = ^{
MyClass *strongSelf = weakSelf;
if (strongSelf) {
strongSelf.topView.frame = CGRectMake(....);
strongSelf.bottomView.frame = CGRectMake(....);
}
};
Run Code Online (Sandbox Code Playgroud)
这实际上是必要的吗?
Jos*_*erg 22
如果self继续持有对块的引用(或者self拥有的东西),那么这里只有一个循环.如果不是你就好了,因为它的生命周期不是由它保留的自我决定的.
所以在你的特定例子中,你似乎很清楚.动画块不需要参加弱/强自我舞蹈.
| 归档时间: |
|
| 查看次数: |
6687 次 |
| 最近记录: |