Search code examples
iosswiftnsoperationnsoperationqueue

Adding operation in `Executing` state as dependency to another one


I'm wondering whether it is possible to add as dependency, operation which is in 'Executing' state to another operation which is enqueued and in 'Ready' state?

e.g. A operation is in 'Executing' state and I want to perform B.addDependency(operationA) (B operation is enqueued and in 'Ready/Pending' state).

From my investigation it is possible but I couldn't find any documentation regarding to this case


Solution

  • Yes, you can add dependencies to a queued operation that has not yet started.

    Note: I know you said that the operation to which you are adding the dependency hasn't started yet, but if it had, the dependency "has no practical effect". For this reason, if possible, you generally would want to add any dependencies before queuing the operation, to avoid race conditions between when the operation starts and when you added the dependency.