One reason for the NSOperation subclass is to implement proper undo. You can make your own approach, but this violates several good design principles. In principle, since cancellation requires the cooperation of the operation itself, NSInvocationOperation not designed to cancel the call during its execution (although it can be successfully canceled before it is launched), since the current method does not need to know anything about its name.
Instead, if you are a subclass of NSOperation , you can easily add most of this function to the main method:
@implementation MyOperation - (void)main { if ([self isCancelled]) return; for (...) { // do stuff if ([self isCancelled]) { [tmp_array release]; return; } } } @end
Also note that you do not need to maintain your own auto-resource pool with this implementation.
source share