Last active
November 27, 2015 12:55
-
-
Save WedgeSparda/972bd5f2818009166b4b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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