2

This documentation says that operations is deprecated with no hint of a replacement function:

https://developer.apple.com/documentation/foundation/nsoperationqueue/1415168-operations?language=objc

Xcode lists a possible replacement as addBarrierBlock: but with no documentation.

I have a dozen operations of class RenderOperation and a single operation of class RenderCompleteOperation that is dependent on all the RenderOperation objects.

My problem is that if I call cancelAllOperations, I still need my single RenderCompleteOperation to run - and if it is still pending completion of its dependencies, than its main method will never run.

So I need a way to cancel just the RenderOperation objects and can't see how to do this without calling operations.

Trygve
  • 1,317
  • 10
  • 27

2 Answers2

1

An operation can be dependant on an operation in a different queue so you can put your final operation on a separate queue to the worker operations and use cancelAllOperations on the worker queue.

Another option is to override the final operation's cancel function to do nothing and set executing and finished manually when it finishes its task.

A third option is to keep an array of your worker operations and cancel each of them yourself in a loop (which is all cancelAllOperations does anyway)

nevan king
  • 112,709
  • 45
  • 203
  • 241
  • Where is it specifically documented that a dependency can be made to an operation in a different queue? This question seems to indicate that isn't supported: https://stackoverflow.com/questions/20576930/nsoperation-with-dependency-on-another-operation-on-a-different-queue-does-not-s – Trygve Jan 04 '20 at 04:29
  • I've never tried dependencies between two queues but I've heard it in WWDC sessions. Here's one place it's mentioned: https://developer.apple.com/videos/play/wwdc2015/226/?time=690 – nevan king Jan 04 '20 at 11:58
  • The question you link to doesn't say it isn't supported, just that they couldn't get it working. Try it yourself and see. Personally I think an array with operations to be cancelled is less hassle (except maybe the hassle of making sure to clean up properly) – nevan king Jan 04 '20 at 11:58
0

You can call cancel on all the dependencies of your RenderCompleteOperation:

renderCompleteOperation.dependencies.forEach { $0.cancel() }

It will then execute immediately.

Antonio Favata
  • 1,891
  • 1
  • 15
  • 13
  • This does work, but it requires that I keep a reference to the completion operation. Not a terrible problem, but not quite as nice as some way to do it using only the queue. – Trygve Dec 31 '19 at 20:29