Search code examples
cocoakey-value-observingnsoperationnsoperationqueue

Marking custom subclass of NSOperation as terminated?


I've created a custom subclass of NSOperation and I've overwritten the main method.

@interface WGTask : NSOperation
@property(readonly) BOOL isExecuting,isFinished;
@end


@implementation WGTask
@synthesize isExecuting,isFinished;
- (void)start {
    ...
    [self willChangeValueForKey:@"isFinished"];
    isFinished=YES;
    [self didChangeValueForKey:@"isFinished"];
    ...
}
@end

But this code raises an EXC_BAD_ACCESS error. Removing [self didChangeValueForKey:@"isFinished"] and [self willChangeValueForKey:@"isFinished"] solves the problem, but even if the isFinished value is correctly updated, the NSOperationQueue doesn't remove the operation!


Solution

  • My fault. Before calling [self willChangeValueForKey:@"isFinished"] I was calling a delegate method of my custom subclass in which I was releasing the task itself. That's why I got the EXC_BAD_ACCESS error, because self didn't exist anymore.