Created
April 4, 2012 21:40
-
-
Save zbowling/2305907 to your computer and use it in GitHub Desktop.
am I crazy?
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
// | |
// SMOperationQueueOperation.m | |
// DorsiaFoundation | |
// | |
// Created by Zac Bowling on 4/4/12. | |
// Copyright (c) 2012 SeatMe, Inc. All rights reserved. | |
// | |
#import "SMOperationQueueOperation.h" | |
@implementation SMOperationQueueOperation { | |
NSOperationQueue *_queue; | |
BOOL _finished; | |
BOOL _executing; | |
} | |
- (id) init { | |
self = [super init]; | |
if (self) { | |
_queue = [[NSOperationQueue alloc] init]; | |
[_queue setMaxConcurrentOperationCount:4]; | |
[_queue setSuspended:YES]; | |
} | |
return self; | |
} | |
- (void)start { | |
if (![self isReady]) | |
return; | |
[self willChangeValueForKey:@"isExecuting"]; | |
_executing = YES; | |
[self didChangeValueForKey:@"isExecuting"]; | |
if ([self isCancelled]) { | |
[self willChangeValueForKey:@"isFinished"]; | |
[self willChangeValueForKey:@"isExecuting"]; | |
_finished = YES; | |
_executing = NO; | |
[self didChangeValueForKey:@"isExecuting"]; | |
[self didChangeValueForKey:@"isFinished"]; | |
return; | |
} | |
__weak SMOperationQueueOperation *weakSelf = self; | |
NSOperation *lastOperation = [NSBlockOperation blockOperationWithBlock:^{ | |
[weakSelf willChangeValueForKey:@"isFinished"]; | |
[weakSelf willChangeValueForKey:@"isExecuting"]; | |
_finished = YES; | |
_executing = NO; | |
[weakSelf didChangeValueForKey:@"isExecuting"]; | |
[weakSelf didChangeValueForKey:@"isFinished"]; | |
}]; | |
for (NSOperation *op in _queue.operations) { | |
[lastOperation addDependency:op]; | |
} | |
[_queue addOperation:lastOperation]; | |
[_queue setSuspended:NO]; | |
} | |
- (void)addOperation:(NSOperation *)operation { | |
if (!_executing && !_finished) { | |
[_queue addOperation:operation]; | |
} | |
} | |
- (BOOL)isConcurrent { | |
return YES; | |
} | |
- (BOOL)isFinished { | |
return _finished; | |
} | |
- (BOOL)isExecuting { | |
return _executing; | |
} | |
- (void)cancel { | |
[super cancel]; | |
[_queue cancelAllOperations]; | |
[self willChangeValueForKey:@"isFinished"]; | |
[self willChangeValueForKey:@"isExecuting"]; | |
_finished = YES; | |
_executing = NO; | |
[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