Search code examples
objective-cconcurrencynsoperationnsoperationqueue

Allow only certain operations to be concurrent


I'm wondering if it's possible to make operations within a NSOperationQueue only concurrent with certain others. For example:

  • Operation A - non-concurrent with all
  • Operation B - can be concurrent with C (but not itself)
  • Operation C - can be concurrent with B (but not itself)

Which could form a queue as below:

    B           B
A - | - B - B - | - A
    C           C

Is such specific concurrency conditions possible with NSOperationQueue?

The concurrent property of NSOperation is misleading in that it rather specifies rather the operation executes its task asynchronously (and in fact is now ignored since 10.6).


Solution

  • This seems to be a good situation for the use of dependencies. With maxConcurrentOperations = 2 the last operation in the queue can be checked before adding another.

    If A, make new operation dependent on that if the new operation is either A, B or C.

    If B, make new operation dependent on that if the new operation is either B or A.

    If C, make new operation dependent on that if the new operation is either C or A.