25

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?

DD_
  • 7,230
  • 11
  • 38
  • 59
lakshmen
  • 28,346
  • 66
  • 178
  • 276
  • 6
    What's wrong with that way? –  Feb 21 '13 at 06:47
  • the only way? any other way? – lakshmen Feb 21 '13 at 06:49
  • Yes, it's the only way. – 一二三 Feb 21 '13 at 06:51
  • 17
    Could someone explain to me why this question was closed? It's not difficult to tell what is being asked here. It's not ambiguous, vague, incomplete, or overly broad. It may be short, but it's to the point. What's wrong with that? I'd personally like to read more than one person's answer. That's not to say that Gabriel's answer is bad. It's really good. I'd just like to hear more information about using this class. So, seriously, are there any other ways of making the `NSOperationQueue` serial? Or to phrase it differently, is there a "best" way to handle a serial queue with a concurrent class? – Ben Stock Sep 28 '14 at 07:20
  • 3
    I agree, this seems like a good question to me. For my part, I make the queue serial by using addDependency: on each added NSOperation, and setting the previously added operation as a dependency for the next one. This ensures that the operations will be run in the order they are added. – Chadwick Wood Jun 08 '15 at 22:11

1 Answers1

20

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.

Gabriel
  • 3,319
  • 1
  • 16
  • 21
  • 1
    do you think it's needed to addDependency chain to each operation? – yershuachu Jul 24 '15 at 10:12
  • 15
    I don't think setting maxConcurrentOperation to 1 makes the operations serial if by serial you mean that the order in the which the operations were queued is maintained. In order to maintain the insertion order, you will have to add dependencies between the operations. NSOpeationQueue is concurrent by default and the order in which the operations are picked for execution is not maintained. Eg: if the operations are queued as 1, 2, 3, 4, then, they may get picked up for execution in the order 2, 3, 1, 4. – Kunal Shrivastava Nov 23 '16 at 00:57
  • 1
    @KunalShrivastava is right. [`If the execution order of your operation objects is important to you, you should use dependencies to establish that order before adding your operations to a queue.`](https://developer.apple.com/library/content/documentation/General/Conceptual/ConcurrencyProgrammingGuide/OperationObjects/OperationObjects.html#//apple_ref/doc/uid/TP40008091-CH101-SW38) – DanSkeel Dec 06 '17 at 19:51
  • 7
    https://developer.apple.com/documentation/foundation/operationqueue "If all of the queued operations have the same queuePriority and are ready to execute when they are put in the queue—that is, their isReady property returns true—they’re executed in the order in which they were submitted to the queue." – Doug Voss Aug 20 '19 at 00:16
  • @KunalShrivastava so what's the proper way to do make operation queue serial ? – Surjeet Rajput Jun 07 '21 at 16:50