You are calling this call in the background. To fix, try something like ...
public var context: NSManagedObjectContext DispatchQueue.main.async { var appDelegate = UIApplication.shared.delegate as! AppDelegate context = appDelegate.persistentContainer.viewContext }
Although this is a pretty bad way to do this ... you use your App Delegate as a global variable (which we all know is bad!)
You should look at passing the context of the managed entity from the view controller to view the controller ...
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: (window?.rootViewController as? MyViewController)?.moc = persistentContainer.viewContext }
etc.
source share