Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 333
Z S Avatar asked Jan 18 '12 01:01

Z S


People also ask

Does Dispatch_ async create a thread?

When using dispatch_async for a background queue, GCD (Grand Central Dispatch) will ask the kernel for a thread, where the kernel either creates one, picks an idle thread, or waits for one to become idle. These threads, once created, live in the thread pool.

How many types of dispatch queues are there?

There are two types of dispatch queues, serial dispatch queues and concurrent dispatch queues.

Which of the following queues are used in Grand Central Dispatch?

Understanding Queues As mentioned before, GCD operates on dispatch queues through a class aptly named DispatchQueue . You submit units of work to this queue, and GCD executes them in a FIFO order (first in, first out), guaranteeing that the first task submitted is the first one started.

How does queue dispatch work?

Dispatch queues are FIFO queues to which your application can submit tasks in the form of block objects. Dispatch queues execute tasks either serially or concurrently. Work submitted to dispatch queues executes on a pool of threads managed by the system.


1 Answers

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.

like image 127
Lily Ballard Avatar answered Oct 14 '22 00:10

Lily Ballard