Reputation: 1602
In my NSOperation subclasses i check for cancellation at top of lengthly loops or before any lengthly methods (Core Data Fetches, Array Sorting etc.) in the subclass's main
method. See an example below.
-(void)main{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
for( int i=0; i< 100; i++ )
{
//Check for cancellation
if( [self isCancelled] ){
[pool drain];
return;
}
//Do the work
}
[pool drain];
}
Is there anything else i ought to be doing, apart from draining the pool, and exiting the method as fast as possible?
Upvotes: 1
Views: 742
Reputation: 104708
1) exit gracefully
2) inform listeners/delegates appropriately. if you have a delegate, you'll want to provide callbacks or interfaces/protocols which support multiple operation results (success, fail or error, cancel, timeout). your listeners will not necessarily be the object which called cancel, but they may have a better idea how to handle such a situation.
3) cleanup your object's state - e.g., release and zero objects which should be programmatically inaccessible, and make sure that any invocation will check isCancelled
if the operation operates in a multithreaded context.
Upvotes: 2