1

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!

Nickkk
  • 2,261
  • 1
  • 25
  • 34

2 Answers2

2

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.

Nickkk
  • 2,261
  • 1
  • 25
  • 34
  • had to re-read this a few times to get it (this also helped http://stackoverflow.com/questions/3291834/asihttprequest-dealloc-and-exc-bad-access-problem) but i ended up finding that my view was "autoreleased" and thats why i was getting my exc_bad_access. – owen gerig Nov 21 '11 at 19:19
1

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

Community
  • 1
  • 1
Joshua Smith
  • 6,561
  • 1
  • 30
  • 28
  • Thank you for the answer, but removing all properties and overwriting the `- (BOOL)isExecuting` and `- (BOOL)isFinished` doesn't seem to work. I still get the EXC_BAD_ACCESS error. Maybe I'm forgetting something. Do I have to implement some other methods to get the KVO working? The main issue is that the operation queue doesn't get notified when the operation terminates. – Nickkk Sep 02 '11 at 16:38
  • this might help http://stackoverflow.com/questions/7805702/how-to-check-if-nsoperationqueue-is-finished-and-if-any-operation-failed – Joshua Smith Oct 01 '12 at 15:27