ARC是否支持调度队列? [英] Does ARC support dispatch queues?

查看:77
本文介绍了ARC是否支持调度队列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读Apple的有关调度队列的内存管理"的文档:

I'm reading apple's documentation about "Memory Management for Dispatch Queues":

即使实现垃圾收集应用程序,也必须仍然保留和释放调度队列和其他调度对象. Grand Central Dispatch不支持垃圾回收模型来回收内存.

Even if you implement a garbage-collected application, you must still retain and release your dispatch queues and other dispatch objects. Grand Central Dispatch does not support the garbage collection model for reclaiming memory.

我知道ARC不是垃圾收集器,但是我想确定我不需要dispatch_retain和dispatch_release我的dispatch_queue_t

I know that ARC is not a garbage collector but I'd like to be sure that I don't need to dispatch_retain and dispatch_release my dispatch_queue_t

推荐答案

简短的答案:是的,ARC保留并释放了调度队列.







而现在,答案很长……

The short answer: YES, ARC retains and releases dispatch queues.







And now for the long answer…

您需要在队列上使用dispatch_retaindispatch_release. ARC不管理它们.

You need to use dispatch_retain and dispatch_release on your queue. ARC does not manage them.

ARC将为您管理队列.如果启用了ARC,则不需要(也不能)使用dispatch_retaindispatch_release.

ARC will manage your queue for you. You do not need to (and cannot) use dispatch_retain or dispatch_release if ARC is enabled.

从iOS 6.0 SDK和Mac OS X 10.8 SDK开始,每个调度对象(包括dispatch_queue_t)也是一个Objective-C对象. <os/object.h>头文件中对此进行了记录:

Starting in the iOS 6.0 SDK and the Mac OS X 10.8 SDK, every dispatch object (including a dispatch_queue_t) is also an Objective-C object. This is documented in the <os/object.h> header file:

 * By default, libSystem objects such as GCD and XPC objects are declared as
 * Objective-C types when building with an Objective-C compiler. This allows
 * them to participate in ARC, in RR management by the Blocks runtime and in
 * leaks checking by the static analyzer, and enables them to be added to Cocoa
 * collections.
 *
 * NOTE: this requires explicit cancellation of dispatch sources and xpc
 *       connections whose handler blocks capture the source/connection object,
 *       resp. ensuring that such captures do not form retain cycles (e.g. by
 *       declaring the source as __weak).
 *
 * To opt-out of this default behavior, add -DOS_OBJECT_USE_OBJC=0 to your
 * compiler flags.
 *
 * This mode requires a platform with the modern Objective-C runtime, the
 * Objective-C GC compiler option to be disabled, and at least a Mac OS X 10.8
 * or iOS 6.0 deployment target.

这意味着您可以将队列存储在NSArrayNSDictionary中,或存储在具有strongweakunsafe_unretainedassignretain属性之一的属性中.这也意味着,如果您从某个块中引用队列,该块将自动保留该队列.

This means you can store your queue in an NSArray or NSDictionary, or in a property with one of the strong, weak, unsafe_unretained, assign, or retain attributes. It also means that if you refer to your queue from a block, the block will retain the queue automatically.

因此如果您的部署目标至少是iOS 6.0 或Mac OS X 10.8,并且您已启用ARC ,则ARC将保留并释放您的队列,并且编译器会将使用dispatch_retaindispatch_release的任何尝试标记为错误.

So if your deployment target is at least iOS 6.0 or Mac OS X 10.8, and you have ARC enabled, ARC will retain and release your queue, and the compiler will flag any attempt to use dispatch_retain or dispatch_release as an error.

如果您的部署目标至少是iOS 6.0 或Mac OS X 10.8,并且您已禁用ARC ,则必须手动保留和释放队列, 通过调用dispatch_retaindispatch_release发送队列retainrelease消息(例如[queue retain][queue release]).

If your deployment target is at least iOS 6.0 or Mac OS X 10.8, and you have ARC disabled, you must manually retain and release your queue, either by calling dispatch_retain and dispatch_release, or by sending the queue retain and release messages (like [queue retain] and [queue release]).

为了与旧代码库兼容,可以通过定义OS_OBJECT_USE_OBJC0来防止编译器将队列视为Objective-C对象.例如,您可以将其放在.pch文件中(在任何#import语句之前):

For compatibility with old codebases, you can prevent the compiler from seeing your queue as an Objective-C object by defining OS_OBJECT_USE_OBJC to 0. For example, you can put this in your .pch file (before any #import statements):

#define OS_OBJECT_USE_OBJC 0

,或者您可以在构建设置中将OS_OBJECT_USE_OBJC=0添加为预处理器宏.如果将OS_OBJECT_USE_OBJC设置为0,则ARC将为您保留或释放您的队列,并且您必须自己使用dispatch_retaindispatch_release来完成.

or you can add OS_OBJECT_USE_OBJC=0 as a preprocessor macro in your build settings. If you set OS_OBJECT_USE_OBJC to 0, ARC will not retain or release your queue for you, and you will have to do it yourself using dispatch_retain and dispatch_release.

这篇关于ARC是否支持调度队列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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