Cancel NSOperation: subclass of NSInvocationOperation or NSOperation?

I have a fairly simple, albeit expensive, task that I need to run in the background, which is a standard NSOperation situation. I also need to make sure that the operation supports cancellation and stops accordingly. Given this requirement, which is a better approach: just wrap an expensive method call in NSInvocationOperation or write an NSOperation subclass from scratch?

Here is my thinking for now. NSInvocationOperations is my first choice, and this is what I used in the past because the task is so simple that I don’t want to write a whole class with the entire NSOperation template template just to execute it. What makes me shy right now is the fact that there really is no way for the method executed in NSInvocationOperation to check for cancellation that does not cause a hacker alarm in my head. See this question for some examples of the mentioned hacker. I tried them and they really work, they just feel bad.

Recording an NSOperation subclass, as mentioned earlier, seems redundant to perform a simple task, but there is no doubt that the cancellation check is more elegant than anything I have seen for NSInvocationOperation.

So, for those who have more NSOperations under their belt, to which you are accustomed to the most successful ending? Is there a great cancellation solution using NSInvocationOperations that I might have missed? Without any support for cancellation, the number of situations in which NSInvocationOperations are useful has simply left the cliff.

+4
source share
2 answers

just a subclass of NSOperation. (more characters required)

+1
source

The NSOperation subclass is the only way to do this, I believe.

I tend to use NSInvocationOperations when I refactor existing code that will be multithreaded, then this is a convenient shortcut.

+2
source

Source: https://habr.com/ru/post/1344185/


All Articles