I have a NSOperationQueue that is concurrent. For a specific NSOperation, if it fails, I want to immediately retry this operation at the highest priority, and suspend all other operations until it succeeded.
I can think of scheduling a operation with higher priority, but how can I make all other operations waiting for this one in an efficient way? Changing all remaining operations dependencies seem too time consuming.
There are a few approaches:
One simple approach, which cuts the Gordian knot, is to just make the task that may require multiple attempts not finish until the retries are done (i.e., incorporate the retry login within the operation, itself). Then schedule the first task with a barrier, schedule the subsequent tasks, and that way none of the subsequent tasks will be able to run until the first one finishes (including all of its retries).
Alternatively, if you want to make the retry tasks separate operations, but do not want to use dependencies, you could add the subsequent tasks to a separate, suspended, queue:
Then, add the task that may require retries to another queue (i.e., obviously, one that is not suspended):
When you do this, the first operation will un-suspend the task queue when the necessary criteria have been met:
For the sake of completeness, the other alternative is to subclass
Operationand make theisReadylogic not only return itssuperimplementation, but also observe some property. E.g.and then