title: Computed Property - How to use only on main thread?
body: <p>I am trying to use an AVCaptureVideoPreviewLayer to capture video, however when I run my application, the main thread checker causes the crash. </p>
  
  <p>At what point must the code be run on the main thread - I am assuming this will resolve the issue I am having. Several attempts have been made using <code>DispatchQueue.main.async({})</code> but to no avail.</p>
  
  <p>Error Occurring: </p>
  
  <blockquote>
    <p>Main Thread Checker: UI API called on a background thread:</p>
  </blockquote>
  
  <p>Original:</p>
  
  <pre><code>    var videoPreviewLayer: AVCaptureVideoPreviewLayer {
  
      let previewlayer = layer as! AVCaptureVideoPreviewLayer
  
      switch gravity {
        case .resize:
          previewlayer.videoGravity = AVLayerVideoGravity.resize
        case .resizeAspect:
          previewlayer.videoGravity = AVLayerVideoGravity.resizeAspect
        case .resizeAspectFill:
          previewlayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
      }
      return previewlayer
     }
  </code></pre>
  
  <p>Attempted Fix:</p>
  
  <pre><code>var videoPreviewLayer: AVCaptureVideoPreviewLayer {
  
    getVideoPreviewLayer { (previewLayer) in
      return previewLayer
    }
  }
  
  func getVideoPreviewLayer( completion: @escaping (AVCaptureVideoPreviewLayer) -&gt; ()) {
  
    DispatchQueue.main.async { [unowned self ] in
  
      let previewlayer = self.layer as! AVCaptureVideoPreviewLayer
  
      switch self.gravity {
      case .resize:
        previewlayer.videoGravity = AVLayerVideoGravity.resize
      case .resizeAspect:
        previewlayer.videoGravity = AVLayerVideoGravity.resizeAspect
      case .resizeAspectFill:
        previewlayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
      }
  
      completion(previewlayer)
    }
  }
  </code></pre>
  
  <hr>
  
  <p>Original
  <a href="https://i.stack.imgur.com/lInaR.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/lInaR.png" alt="enter image description here"></a>
  Attempted Fix
  <a href="https://i.stack.imgur.com/qNNBa.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/qNNBa.png" alt="enter image description here"></a></p>