ARC、块和保留周期 [英] ARC, Blocks and Retain Cycles

查看:31
本文介绍了ARC、块和保留周期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 ARC 处理面向 4.0 和 5.0 的 iOS 项目.

Working on an iOS project that targets 4.0 and 5.0, using ARC.

遇到与块、ARC 和从块外引用对象相关的问题.这是一些代码:

Running into an issue related to blocks, ARC and referencing an object from outside the block. Here's some code:

 __block AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
   [operation setCompletionBlock:^ {
       if ([operation isCancelled]) {
           return;
       }

... do stuff ...

operation = nil;
}];

在这种情况下,编译器会发出警告,在块中使用操作"将导致保留循环.在 ARC 下,__block 现在保留了变量.

In this case, the compiler gives a warning that using 'operation' in the block is going to lead to a retain cycle. Under ARC, __block now retains the variable.

如果我添加 __unsafe_unretained,编译器会立即释放该对象,所以显然这不起作用.

If I add __unsafe_unretained, the compiler releases the object immediately, so obviously that won't work.

我的目标是 4.0,所以我不能使用 __weak.

I'm targeting 4.0 so I can't use __weak.

我试着做这样的事情:

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
__block __unsafe_unretained AFHTTPRequestOperation *weakOperation = operation;

但是虽然 weakOperation 不是 nil,但它的任何属性都不会在块内填充.

but while weakOperation isn't nil, none of it's properties are populated when inside the block.

鉴于上面列出的项目限制,处理这种情况的最佳方法是什么?

What's the best way to handle this situation given the project constraints listed above?

推荐答案

假设进度保证,保留周期可能正是您想要的.您在块结束时显式中断保留循环,因此它不是永久保留循环:当调用块时,循环被中断.

Assuming progress guarantees, a retain cycle might be exactly what you want. You explicitly break the retain cycle at the end of the block, so it's not a permanent retain cycle: when the block is called, the cycle is broken.

但是,如果您有其他东西保持操作,您可以将引用存储到 __weak__unsafe_unretained 变量中,然后在您的块中使用它.除非您出于某种原因需要在块期间更改变量的绑定,否则无需对变量进行 __block 限定;由于您不再需要中断循环,因此您不需要为弱变量分配任何内容.

If you have something else keeping the operation around, though, you can store a reference into either a __weak or __unsafe_unretained variable and then use that from within your block. There's no need to __block-qualify the variable unless you for some reason need to change the variable's binding during the block; since you don't have a retain cycle to break any more, you shouldn't need to assign anything to the weak variable.

这篇关于ARC、块和保留周期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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