Skip to content

Instantly share code, notes, and snippets.

@jmcd
Created September 20, 2013 12:29
Show Gist options
  • Save jmcd/6636739 to your computer and use it in GitHub Desktop.
Save jmcd/6636739 to your computer and use it in GitHub Desktop.
An NSOperation that controls its own finish - useful for operations that use async code (NSConnection etc.). Override start (and call base), then call [self finish] when done.
#import <Foundation/Foundation.h>
@interface XOp : NSOperation
- (void)finish;
@end
#import "XOp.h"
@implementation XOp {
BOOL _isExecuting;
BOOL _isFinished;
}
- (void)start {
[self willChangeValueForKey:@"isExecuting"];
_isExecuting = YES;
[self didChangeValueForKey:@"isExecuting"];
}
- (BOOL)isConcurrent {
return NO;
}
- (BOOL)isExecuting {
return _isExecuting;
}
- (BOOL)isFinished {
return _isFinished;
}
- (void)finish {
[self willChangeValueForKey:@"isExecuting"];
[self willChangeValueForKey:@"isFinished"];
_isExecuting = NO;
_isFinished = YES;
[self didChangeValueForKey:@"isExecuting"];
[self didChangeValueForKey:@"isFinished"];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment