Skip to content

Instantly share code, notes, and snippets.

@yfrancis
Created September 29, 2012 18:59
Show Gist options
  • Save yfrancis/3804904 to your computer and use it in GitHub Desktop.
Save yfrancis/3804904 to your computer and use it in GitHub Desktop.
- (void)addTask:(GRTask*)task
importer:(Class<GRImporter>)Importer
resources:(NSArray*)requiredResources
completionBlock:(GRImportCompletionBlock)complete
{
NSDictionary* taskInfo = [task dictionaryRepresentation];
GRImportCompletionBlock done = [complete copy];
[self addOperationWithBlock:^{
// ask plugin to generate an import block for this task
GRImportOperationBlock import;
import = [Importer newImportBlockWithInfo:taskInfo];
// acquire resource locks
[GRResource acquireResources:requiredResources];
// execute import block generated by plugin
NSError* error = nil;
BOOL status = import(taskInfo, &error);
// unlock resources
[GRResource relinquishResources:requiredResources];
// execute the completion block
done(status, error);
// clean up
[complete release];
[import release];
}];
}
- (void)addTask:(GRTask*)task
importer:(Class<GRImporter>)Importer
resources:(NSArray*)requiredResources
successBlock:(GRImportCompletionBlock)succ
failureBlock:(GRImportCompletionBlock)fail
{
// get execution block for the import task
NSDictionary* taskInfo = [[task dictionaryRepresentation] copy];
GRImportOperationBlock block;
block = [Importer newImportBlockWithInfo:taskInfo
successBlock:succ
failureBlock:fail];
// create NSBlockOperation wrapper
NSBlockOperation* op = [NSBlockOperation blockOperationWithBlock:block];
// set up dependencies
// check task resource requirements
NSMutableArray* savedDepArrays = [NSMutableArray array];
for (NSString* resource in requiredResources) {
NSMutableArray* depends = [resources objectForKey:resource];
if (depends == nil) {
depends = [NSMutableArray array];
[resources setObject:depends forKey:resource];
}
else {
for (NSBlockOperation* dep in depends)
[op addDependency:dep];
}
[depends addObject:op];
[savedDepArrays addObject:depends];
}
[op setCompletionBlock:^{
Block_release(succ);
Block_release(fail);
Block_release(block);
[taskInfo release];
// remove operation from dependency arrays
[savedDepArrays makeObjectsPerformSelector:@selector(removeObject:)
withObject:op];
}];
[self addOperation:op];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment