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.
source share