TL;DR:dispatch_queue_t 现在是一个 Objective C 对象,可以使用 ARC 进行管理。
我还没有测试到这有多远,但是使用 iOS 7 SDK 和 Xcode 5,dispatch_queue_t 是一个对象类型。我将队列的属性声明为
@property (nonatomic, strong) dispatch_queue_t syncQueue;
编译器很高兴,一切都按预期工作。我明确知道这在 iOS 4 或 5 中是行不通的(在 ARC 之前,它是 retain 而不是 strong)。我深入研究了dispatch_queue_t 的定义,发现了这个:
/*!
* @typedef dispatch_queue_t
*
* @abstract
* Dispatch queues invoke blocks submitted to them serially in FIFO order. A
* queue will only invoke one block at a time, but independent queues may each
* invoke their blocks concurrently with respect to each other.
*
* @discussion
* Dispatch queues are lightweight objects to which blocks may be submitted.
* The system manages a pool of threads which process dispatch queues and
* invoke blocks submitted to them.
*
* Conceptually a dispatch queue may have its own thread of execution, and
* interaction between queues is highly asynchronous.
*
* Dispatch queues are reference counted via calls to dispatch_retain() and
* dispatch_release(). Pending blocks submitted to a queue also hold a
* reference to the queue until they have finished. Once all references to a
* queue have been released, the queue will be deallocated by the system.
*/
DISPATCH_DECL(dispatch_queue);
听上去,它应该不起作用,所以我检查了DISPATCH_DECL 的定义,发现了这个,它解释了一切:
/*
* By default, dispatch 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.
* See <os/object.h> for details.
*/