How to make the NSOperationQueue serial?

lakesh picture lakesh · Feb 21, 2013 · Viewed 13.1k times · Source

I am intending to make the NSOperationQueue serial instead of concurrent.

One way I know is:

NSOperationQueue *globalQueue;
globalQueue.maxConcurrentOperationCount =1;

Is there any other way?

Answer

Gabriel picture Gabriel · Feb 21, 2013

If you want a serial queue, you are right setting maxConcurrentOperation to one. You can also use [NSOperationQueue mainQueue] instead of creating a new queue, and so queue operations on the main thread. But that is only useful if very short operations are added and so the user interface is not blocked. And on the other hand you have not to worry about threads n synch.

You can add operations to any queue with addOperations:waitUntilFinished:YES or sending the message waitUntilAllOperationsAreFinished every time you add an operation. That way you are serializing operations instead of defining the queue as serial.