Created
May 29, 2013 17:46
-
-
Save enigmaticape/5672229 to your computer and use it in GitHub Desktop.
I needed a quick spike to demonstrate queueing N asynchronous tasks on a GCD queue. So here it is, just in case you ever need to, you know, asynchronously execute N muppets in batches of M. Well, you never know.
This file contains hidden or 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
// | |
// AppDelegate.m | |
// GCD_SPIKE_2 | |
// | |
// Created by Steve Trewick on 29/05/2013. | |
// Copyright (c) 2013 Enigmatic Ape. All rights reserved. | |
// | |
#import "AppDelegate.h" | |
#import <dispatch/dispatch.h> | |
@implementation AppDelegate { | |
dispatch_queue_t _q; | |
dispatch_group_t _group; | |
NSMutableArray * _wokkas; | |
} | |
-(void) moar_muppets { | |
for( int i = 0; i < 3; i++ ) { | |
if( _wokkas.count == 0 ) { | |
break; | |
} | |
NSString * wokka = [_wokkas lastObject]; | |
[_wokkas removeLastObject]; | |
dispatch_group_async(_group, _q, ^{ | |
NSLog(@"%@ (%i)",wokka, i); | |
}); | |
} | |
dispatch_group_notify(_group, _q, ^{ | |
[self group_done]; | |
}); | |
} | |
-(void) group_done { | |
if( _wokkas.count == 0) { | |
NSLog(@"We'll see you next time on the muppet show"); | |
} | |
else { | |
NSLog(@"MOAR!"); | |
[self moar_muppets]; | |
} | |
} | |
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification | |
{ | |
_group = dispatch_group_create(); | |
_q = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0); | |
_wokkas = [[NSMutableArray alloc] init]; | |
for( int i = 0; i < 13; i ++ ) { | |
[_wokkas addObject:@"wokka" ]; | |
} | |
[self moar_muppets]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment