enumerateObjectsUsingBlock and completion together
<p>I have situation where I am iterating my array objects using enumerateObjectsUsingBlock, and I need to wait for a completion in the iteration then it should execute further code, how can I achieve this, or if any alternate I should use, below is my code</p>

<pre><code>[arrPendingQueue enumerateObjectsUsingBlock:^(PendingQueues *obj, NSUInteger idx, BOOL *stop) {

       //Fetch static google map based on coordinates
       [self generateMapImage:obj.postObj completion:^(UIImage *image) {
           NSString *path = [NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"camera"];

          NSString *resultPath = [path stringByAppendingPathComponent:[NSString stringWithFormat:@"map_%f.jpg",[[Constants getSystemDateInLocalTimeZone] timeIntervalSince1970]]];

          [[NSFileManager defaultManager] createFileAtPath:resultPath contents:UIImageJPEGRepresentation(image, 1.0) attributes:nil];

          postObj.imagepath = [@"camera" stringByAppendingPathComponent:[resultPath lastPathComponent]];

          [DataManager saveObject:postObj];                                             
       }];

       //After the completion I want to upload the data here to server

});
</code></pre>

<p>Basically, I am trying to download static google map from coordinate in function <code>generateMapImage</code> and I want the loop to wait till completion called..</p>

<p>For that I tried to use <code>dispatch_semaphore_create</code> like</p>

<pre><code>[arrPendingQueue enumerateObjectsUsingBlock:^(PendingQueues *obj, NSUInteger idx, BOOL *stop) {
     dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);

     [self generateMapImage:obj.postObj completion:^(UIImage *image) {

         dispatch_semaphore_signal(semaphore);                                    
     }];

     dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);

});
</code></pre>

<p>But it doesn't work it doesn't wait, and executes the code before completion completed.</p>

<p>How to solve, please help.</p>

<p>Thanks.</p>