2

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

Robert
  • 3,790
  • 1
  • 27
  • 46
  • May I ask why you're using `NSOperationQueue`? You'll have a much easier time using `DispatchQueue`. It's API was specifically improved for Swift, it's more widely used, and is more flexible (after all, `NSOperationQueue` is just a wrapper over `DispatchQueue` with less features) – Alexander May 18 '17 at 21:29
  • yes Im using OperationQueue, I know that DispatchQueue is wrapped by OperationQueue but I asked only for my curiosity whether it is possible :) – Robert May 18 '17 at 21:35
  • 1
    @Alexander - There are tons of things that operation queues make much easier than dispatch queues. Not only adding dependencies (which this question was about), but wrapping tasks (especially asynchronous ones) in nice, discrete operations; constraining degree of concurrency; etc. I agree that dispatch queues are often the "go to" tool, especially for simple stuff, but there are tons of jobs that are much better suited for operation queues. – Rob May 18 '17 at 22:44

1 Answers1

1

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.

Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • The other limitations are that one should avoid adding dependency between two operations more than once and that you should avoid circular dependencies (where A is dependent on B and B is dependent on A). – Rob May 18 '17 at 23:02