title: UIApplication.delegate must be used from main thread only
body: <p>I have the following code in my app delegate as a shortcut for working with CoreData in my other viewControllers:</p>
  
  <pre><code>let ad = UIApplication.shared.delegate as! AppDelegate
  let context = ad.persistentContainer.viewContext
  </code></pre>
  
  <p>However, I now get the error message: </p>
  
  <blockquote>
    <p>"UI API called from background thread" and "UIApplication.delegate must be used from main thread only". </p>
  </blockquote>
  
  <p>I am working with CoreData while my app is in the background, but this is the first time I've seen this error message. Does anybody know what's going on here?</p>
  
  <p>Update:  I tried to move this inside the appDelegate class itself, and using the following code - </p>
  
  <pre><code>let dispatch = DispatchQueue.main.async {
      let ad = UIApplication.shared.delegate as! AppDelegate
      let context = ad.persistentContainer.viewContext
  }
  </code></pre>
  
  <p>Now, I can no longer access the ad and context variables outside the <code>AppDelegate</code>. Is there something I'm missing?</p>