Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
440 views
in Technique[技术] by (71.8m points)

objective c - What property should I use for a Dispatch Queue after ARC?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...