title: Prevent loop from looping until inner loop writes to disk
body: <p>I have a nested for loop in where I call a <code>getSnapShotData</code> method many times and write this data to disk. I've noticed I get too much memory build up doing this and I run out of memory, so I thought this would be a good use case for using <code>dispatch semaphore</code>.</p>
  
  <p>I'm still running out of memory, so I'm not sure if I'm using the semaphore properly. Essentially I want the next loop to wait until the prior loop's data is written to disk, as I think that will free the memory. But I could be wrong. Thank you for your help.</p>
  
  <p>Code:</p>
  
  <pre><code>dispatch_semaphore_t sema = dispatch_semaphore_create(0);
  
  for (NSDictionary *sub in self.array)
  {
      NSArray *lastArray = [sub objectForKey:@"LastArray"];
  
      for (NSDictionary *dict in lastArray)
      {
          currentIndex ++;
  
          NSData *frame = [NSData dataWithData:[self getSnapshotData]];
  
          savePath = [NSString stringWithFormat:@"%@/%lu.png",frameSourcePath,(unsigned long)currentIndex];
  
          BOOL nextLoop = [frame writeToFile:savePath options:0 error:nil];
  
          frame = nil;
  
          if (nextLoop)
          {
              dispatch_semaphore_signal(sema);
          }
  
          dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
      }
  }
  
  - (NSData *)getSnapshotData
  {
      UIGraphicsBeginImageContextWithOptions(self.containerView.bounds.size, NO, 0.0);
      [self.containerView.layer renderInContext:UIGraphicsGetCurrentContext()];
      UIImage *snapShot = UIGraphicsGetImageFromCurrentImageContext();
      UIGraphicsEndImageContext();
  
      return [NSData dataWithData:UIImagePNGRepresentation(snapShot)];
  }
  </code></pre>