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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With