ARC 之后我应该为调度队列使用什么属性?
我将调度队列作为视图控制器的属性来维护。我在视图控制器的 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
更新的答案:
在当前的 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对象,因此唯一可能的选择是
分配
。如果您尝试使用strong
或weak
,编译器将抛出错误。 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 to1
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 at0
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 usestrong
orweak
. ARC has no impact on GCD.下面是如何为 iOS 6.0 及更高版本和 iOS 6.0 以下定义dispatch_queue_t 属性
基本上,对于 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
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.
这是我使用的:
Here is what I use:
基于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.
TL;DR:
dispatch_queue_t
现在是一个 Objective C 对象,可以使用 ARC 进行管理。我还没有测试过多久以前这是真的,但使用 iOS 7 SDK 和 Xcode 5,
dispatch_queue_t
是一种对象类型。我正在声明队列的属性,因为编译器很高兴并且一切都按预期工作。我明确知道这过去在 iOS 4 或 5 中不起作用(在 ARC 之前,它是
retain
而不是strong
)。我深入研究了dispatch_queue_t
的定义并发现了这一点:听上去,它不应该起作用,所以我检查了
DISPATCH_DECL
的定义并发现了这一点,这解释了一切: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 asThe 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 ofstrong
). I dug into the definition fordispatch_queue_t
and found this:By the sounds of that, it shouldn't work, so I checked the definition of
DISPATCH_DECL
and found this, which explains everything: