title: Swift UIApplication.delegate must be used from main thread only
body: <p>Xcode is complaining when I try to set the appDelegate &amp; context variables, required in order to use CoreData.</p>
  <p>Essentially, I would like to store the results of my Vision / CoreML image classification request into a Core Data database for offline analysis.</p>
  <p>Seen related threads to this, and tried a bunch. Problem does not go away, and now (unknown to me!) the CoreData save errors after few hundred records. I am hoping removing this issue altogether will solve the error problem or I can troubleshoot it later...</p>
  <p>This is specifically for debugging, and won't likely need CoreData when data analyses has finished.</p>
  <p>Tried putting the variable declarations right at the top of the ViewController class, with appending &quot;!&quot; as I know I will be setting them later. Tried putting the 2 lines in a DispatchQueue.main.async closure.</p>
  <p>Tried wrapping these 2 lines inside a &quot;DispatchQueue.main.async({ })&quot; line, but then I can no longer reference the context on the &quot;newItem&quot; lines. Wrapping the whole section does not work either, probably as the CoreData cannot see / access the data within the image request(?)</p>
  <p>The code:</p>
  <pre><code>func processCameraBuffer(sampleBuffer: CMSampleBuffer) {
      
      let coreMLModel = Inceptionv3()
      
      if let model = try? VNCoreMLModel(for: coreMLModel.model) {
          let request = VNCoreMLRequest(model: model, completionHandler: { (request, error) in
              if let results = request.results as? [VNClassificationObservation] {
                  var counter = 1
                  
                  for classification in results {
                      
                          let timestamp = NSDate().timeIntervalSince1970
                      
                      // Purple Error is here
                      let appDelegate = UIApplication.shared.delegate as! AppDelegate
                      let context = appDelegate.persistentContainer.viewContext
                      
                      let newItem = NSEntityDescription.insertNewObject(forEntityName: &quot;Predictions&quot;, into: context)
                          newItem.setValue(classification.confidence, forKey: &quot;confidence&quot;)
                          newItem.setValue(classification.identifier, forKey: &quot;identifier&quot;)
                          newItem.setValue(counter, forKey: &quot;index&quot;)
                          newItem.setValue(timestamp, forKey: &quot;timestamp&quot;)
                          newItem.setValue(&quot;inceptionv3&quot;, forKey: &quot;mlmodel&quot;)
                          print(&quot;counter: \(counter) \(classification.identifier)&quot;)
                          do {
                              try context.save()
                          } catch {
                              print(&quot;CoreData Save error&quot;)
                              print(error.localizedDescription)
                          }
                      counter += 1
                  }
              }
          })
          
              if let pixelBuffer: CVPixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) {
                  let handler = VNImageRequestHandler(cvPixelBuffer: pixelBuffer, options: [:])
                  do {
                      try handler.perform([request])
                  } catch {
                      print(error.localizedDescription)
                  }
              }
      }
  }
  </code></pre>