ARC 之后我应该为调度队列使用什么属性?

发布于 2024-12-28 01:48:28 字数 478 浏览 3 评论 0原文

我将调度队列作为视图控制器的属性来维护。我在视图控制器的 init 方法中创建此队列一次,并针对某些后台任务重复使用几次。在ARC之前,我是这样做的:

@property (nonatomic, assign) dispatch_queue_t filterMainQueue;

在init中:

if (filterMainQueue == nil) {
     filterMainQueue = dispatch_queue_create("com.myQueue.CJFilterMainQueue", NULL);
}

但是在ARC之后,我不确定这是否应该仍然是“分配”,或者应该是“强”还是“弱”。 ARC 转换器脚本没有更改任何内容,但我不确定是否存在一个微妙的错误,因为该队列在使用时可能会被释放?

使用 ARC 时,这 3 种类型的属性之间有什么区别,什么最适合调度队列?

I maintain a dispatch queue as a property with my view controller. I create this queue once in my view controller's init method, and reuse a few times for some background tasks. Before ARC, I was doing this:

@property (nonatomic, assign) dispatch_queue_t filterMainQueue;

And in init:

if (filterMainQueue == nil) {
     filterMainQueue = dispatch_queue_create("com.myQueue.CJFilterMainQueue", NULL);
}

But after ARC, I'm not sure if this should still be "assign", or should it be "strong" or "weak". The ARC convertor script didn't change anything but I'm not sure if a subtle bug is coming from the fact that this queue might be deallocated while it's being used?

What would be the difference between the 3 types of properties, and what will work the best for a dispatch queue, when using ARC?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

寄人书 2025-01-04 01:48:28

更新的答案:

在当前的 OS X 和 iOS 中,Dispatch 对象现在被 ARC 视为 Obj-C 对象。它们将以与 Obj-C 对象相同的方式进行内存管理,并且您应该为您的属性使用 strong

这是由 中定义的 OS_OBJECT_USE_OBJC 宏控制的。当您的部署目标是 OS X 10.8 或更高版本,或者 iOS 6.0 或更高版本时,它默认设置为 1。如果您要部署到较旧的操作系统,则此值保留为 0,您应该在下面看到我的原始答案。


原始答案:

调度对象(包括队列)不是Obj-C对象,因此唯一可能的选择是分配。如果您尝试使用strongweak,编译器将抛出错误。 ARC对GCD没有影响。

Updated answer:

In current OS X and iOS, Dispatch objects are now treated as Obj-C objects by ARC. They will be memory-managed the same way that Obj-C objects will, and you should use strong for your property.

This is controlled by the OS_OBJECT_USE_OBJC macro, defined in <os/object.h>. It's set to 1 by default when your deployment target is OS X 10.8 or higher, or iOS 6.0 or higher. If you're deploying to an older OS, then this is left at 0 and you should see my original answer below.


Original answer:

Dispatch objects (including queues) are not Obj-C objects, so the only possible choice is assign. The compiler will throw an error if you try to use strong or weak. ARC has no impact on GCD.

◇流星雨 2025-01-04 01:48:28

下面是如何为 iOS 6.0 及更高版本和 iOS 6.0 以下定义dispatch_queue_t 属性

#if OS_OBJECT_HAVE_OBJC_SUPPORT == 1
@property (nonatomic, strong) dispatch_queue_t serialDispatchQueue;
#else
@property (nonatomic, assign) dispatch_queue_t serialDispatchQueue;
#endif

基本上,对于 iOS 6.0 及更高版本,OS_OBJECT_HAVE_OBJC_SUPPORT 被定义为 1。 (MAC 10.8 及以上)。在 iOS 6 以下,它被定义为 0。

OS_OBJECT_HAVE_OBJC_SUPPORT 定义像 GCD 这样的 OS 对象具有 Objective C 支持。所以ARC、内存管理、引用计数等都适用于GCD对象。

Here is how one would define dispatch_queue_t property for iOS 6.0 and above AND below iOS 6.0

#if OS_OBJECT_HAVE_OBJC_SUPPORT == 1
@property (nonatomic, strong) dispatch_queue_t serialDispatchQueue;
#else
@property (nonatomic, assign) dispatch_queue_t serialDispatchQueue;
#endif

Basically OS_OBJECT_HAVE_OBJC_SUPPORT is defined as 1 for iOS 6.0 and above. (MAC 10.8 and above). Below iOS 6 it is defined as 0.

OS_OBJECT_HAVE_OBJC_SUPPORT defines that OS objects like GCD have objective C support. So ARC, memory management, reference counting etc. applies to GCD objects.

温柔戏命师 2025-01-04 01:48:28

这是我使用的:

@property (readwrite, strong, nonatomic) __attribute__((NSObject)) dispatch_queue_t queue;

Here is what I use:

@property (readwrite, strong, nonatomic) __attribute__((NSObject)) dispatch_queue_t queue;
一杯敬自由 2025-01-04 01:48:28

基于iOS7,我测试了dispatch_queue对象是否是objective-C对象,我发现它们已经是objective-c对象。换句话来说,现在不需要 attribute((NSObject)) 。

Based on iOS7, I tested whether dispatch_queue object is an objective-C object and I figured out they already are objective-c object. to paraphrase this, attribute((NSObject)) is not necessary now.

倾`听者〃 2025-01-04 01:48:28

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.
 */

TL;DR: dispatch_queue_t is an Objective C object now and can be managed with ARC.

I haven't tested how far back this is true, but using the iOS 7 SDK and Xcode 5, dispatch_queue_t is an object type. I am declaring a property for a queue as

@property (nonatomic, strong) dispatch_queue_t syncQueue;

The compiler is happy and everything works as expected. I know definitively that this used to not work in iOS 4 or 5 (pre-ARC it was retain instead of strong). I dug into the definition for dispatch_queue_t and found this:

/*!
 * @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);

By the sounds of that, it shouldn't work, so I checked the definition of DISPATCH_DECL and found this, which explains everything:

/*
 * 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.
 */
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文