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

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

问题描述

我正在阅读苹果关于调度队列的内存管理"的文档:

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 对象.这记录在 头文件中:

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 中,或者存储在具有 strong 之一的属性中弱unsafe_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_OBJC 定义为 0 来防止编译器将您的队列视为 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天全站免登陆