Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting main queue as underlying queue for NSOperationQueue

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?

like image 998
Said Sikira Avatar asked Oct 19 '22 03:10

Said Sikira


1 Answers

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.

like image 98
Paulw11 Avatar answered Nov 03 '22 22:11

Paulw11