开发者

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,isFin开发者_运维知识库ished;
@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!


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.


Don't create isExecuting et al as a property

From the docs:

If you are implementing a concurrent operation, you should override this method to return the execution state of your operation. If you do override it, be sure to generate KVO notifications for the isExecuting key path whenever the execution state of your operation object changes. For more information about manually generating KVO notifications, see Key-Value Observing Programming Guide.

Really, you probably want to use the cancel semantics of an NSOperation

http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/NSOperation_class/Reference/Reference.html

You may also wish to read

NSoperation and key value observing

and (if you are using these flags for dependency management)

NSOperation KVO problem

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜