5

NSOperationQueue class has an underlyingQueue property which is used to set the dispatch queue on which NSOperation instances will be executed.

let dispatchQueue = dispatch_queue_create("custom.queue", DISPATCH_QUEUE_SERIAL)
let opQueue = NSOperationQueue()
opQueue.underlyingQueue = dispatchQueue

However, official documentation states that:

The value of this property must not be the value returned by dispatch_get_main_queue

It seems that there is no more explanation on this subject from Apple. However, using main queue as underlyingQueue does not raises any errors nor does it yields any unwanted behavior. What is the reasoning behind this?

Said Sikira
  • 4,482
  • 29
  • 42
  • If you want to use the main queue, why call `dispatch_queue_create`? Just use `dispatch_get_main_queue`. – rmaddy Aug 21 '16 at 21:01
  • `dispatch_queue_create` is used only as an example on how to set `underlyingQueue` property. As you can see from documentation `dispatch_get_main_queue` should not be used as underlying queue. – Said Sikira Aug 21 '16 at 21:04

1 Answers1

3

There could be a number of reasons; the potential for deadlocks springs to mind as one, but you would need to ask Apple for the definitive answer.

If your entire operation must excute on the main queue then you can get the operation queue associated with the main thread by using NSOperationQueue.mainQueue. If only part of your operation needs to execute on the main queue (such as updating UI elements) then you can just dispatch that operation onto the main queue either synchronously or asynchronously as required.

Paulw11
  • 108,386
  • 14
  • 159
  • 186