title: How to stop AVCaptureSession properly
body: <p>I'm doing an iOS application that need verification on QR Code with the Hierarchy like this:</p>
  
  <pre><code>View
  ---Scan View
  ---Image View - cardBG
  ---Inside View
  </code></pre>
  
  <ol>
  <li>When view is loading, the Scan View is hidden.</li>
  <li>When user click button to scan, the Inside View and Image View is set hidden, reveal the Scan View.</li>
  <li>After the scanning return success, the Inside and Image appear again.</li>
  </ol>
  
  <p>The problem is in step 3: when I stop AVCaptureSession, even in an asynchronous dispatch like <a href="https://stackoverflow.com/questions/11905505/avcapturesession-stop-running-take-a-long-long-time">in this question</a>, the it takes 8-10 seconds to refresh the view.</p>
  
  <pre class="lang-c prettyprint-override"><code>dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
      if([_captureSession isRunning])[_captureSession stopRunning];
      AVCaptureInput* input = [_captureSession.inputs objectAtIndex:0];
      [_captureSession removeInput:input];
      AVCaptureVideoDataOutput* output = (AVCaptureVideoDataOutput*)[_captureSession.outputs objectAtIndex:0];
      [_captureSession removeOutput:output];
  });
  
  [self.bgImageView setHidden:NO];
  [self.insideView setHidden:NO];
  [self.scanView setHidden:YES];
  [self.previewLayer removeFromSuperlayer];
  </code></pre>
  
  <p>My question is: How can I get the view to avoid this freeze?</p>