Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- for(NSInteger i = 0; i < 10; i++) {
- // enqueue operation in queue
- dispatch_async(queue, ^{
- // create semaphore
- dispatch_semaphore_t sema = dispatch_semaphore_create(0);
- // do something async, I do use another dispatch_queue for example
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
- // wrap in autoreleasepool to release memory upon completion
- // in your case wrap the resultBlock in autoreleasepool
- @autoreleasepool {
- // here for example the nested operation sleeps for two seconds
- sleep(2);
- // add the operation result to array
- // I construct an array of strings for example
- [results addObject:[NSString stringWithFormat:@"Operation %d has finished.", i]];
- // signal that nested async operation completed
- // to wake up dispatch_semaphore_wait below
- dispatch_semaphore_signal(sema);
- }
- });
- // wait until the nested async operation signals that its finished
- dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
- NSLog(@"Finished single operation.");
- });
- }
- // will be called once all operations complete
- dispatch_async(queue, ^{
- NSLog(@"Finished all jobs.");
- NSLog(@"Results: %@", results);
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement