ARC、块和保留周期

ARC, Blocks and Retain Cycles(ARC、块和保留周期)
本文介绍了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、块和保留周期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Get an event when UIBarButtonItem menu is displayed(显示UIBarButtonItem菜单时获取事件)