Skip to content

Instantly share code, notes, and snippets.

@WedgeSparda
Last active November 27, 2015 12:55
Show Gist options
  • Save WedgeSparda/972bd5f2818009166b4b to your computer and use it in GitHub Desktop.
Save WedgeSparda/972bd5f2818009166b4b to your computer and use it in GitHub Desktop.
// You need to subclass this and populate the main method
// To complete the operation call [self completeOperation]
#import <Foundation/Foundation.h>
@interface AsyncOperation : NSOperation
- (void)completeOperation;
@end
#import "AsyncOperation.h"
@interface AsyncOperation()
{
BOOL _executing;
BOOL _finished;
}
@end
@implementation AsyncOperation
#pragma mark - Init
- (instancetype)init
{
if (self = [super init]) {
_executing = NO;
_finished = NO;
}
return self;
}
#pragma mark - Async Operation
- (void)start
{
if ([self isCancelled]) {
[self willChangeValueForKey:@"isFinished"];
_finished = YES;
[self didChangeValueForKey:@"isFinished"];
return;
}
[self willChangeValueForKey:@"isExecuting"];
[NSThread detachNewThreadSelector:@selector(main) toTarget:self withObject:nil];
_executing = YES;
[self didChangeValueForKey:@"isExecuting"];
}
- (void)completeOperation
{
NSLog(@"END OF OPERATION");
[self willChangeValueForKey:@"isFinished"];
[self willChangeValueForKey:@"isExecuting"];
_executing = NO;
_finished = YES;
[self didChangeValueForKey:@"isExecuting"];
[self didChangeValueForKey:@"isFinished"];
}
- (BOOL)isConcurrent { return YES; }
- (BOOL)isFinished { return _finished; }
- (BOOL)isExecuting { return _executing; }
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment