title: How to wait till a function is completely executed when multiple objects call a function in iOS?
body: <p>I have an architecture where I have to call a local function to display an Image and then in the background need to upload the image to server so that once the Uploading is finished I can remove the local path used for displaying the image.</p>
  
  <p>Functions</p>
  
  <p><code>DidCmpletePickingImage()</code> , <code>DisplayImageUsingLocalPath()</code> , <code>UploadImageToServer()</code> and <code>RemoveImageFromLocal()</code>.</p>
  
  <p>These are the activities. Now i have option to upload multiple images.</p>
  
  <p>This is my current approach. I pick an array of images from and call The function to show them using local path.</p>
  
  <pre><code>for (NSInteger i = 0;i &lt; photos.count ; i ++){
  
                  UIImage *img = photos[i];
                  img = [self imageWithImage:img scaledToWidth:400];
                  NSData *imageData = UIImageJPEGRepresentation(img, 0.40);
                  [self showLocally:imageData img:img];
              }
  </code></pre>
  
  <p>After they are showed I start uploading them to server on background thread</p>
  
  <pre><code>-(void) showLocally:(NSData *)imageData img:(UIImage *)img{
  
      // Code for showing it using temp path.
  
      dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul);
      dispatch_async(queue, ^{
          [self fileUpload:img];
      });
  }
  </code></pre>
  
  <p>Then using the background thread File is uploaded to the server using AFNetwork and then after I get a  response I call remove local file path.</p>
  
  <p>But when I do this calling on Background all the images are being simultaneously calling <code>fileUpload</code> method and doing it <code>concurrently</code> which is increasing load on server. How can I block a call of the function till a the previous object which called the function is completely executed ?</p>