Swift 4 ,must be used from main thread only warning

爱⌒轻易说出口 提交于 2019-12-03 01:12:41

You're making this call on a background queue. 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're using your App Delegate as a global variable (which we all know is bad!)

You should look at passing the managed object context from view controller to view controller…

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: 

    (window?.rootViewController as? MyViewController)?.moc = persistentContainer.viewContext
}

and so on

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!