xcode UIApplication.delegate 只能在主线程中使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47203385/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 10:25:32  来源:igfitidea点击:

UIApplication.delegate must be used from main thread only

iosswiftxcodethread-safetyuiapplication

提问by LFHS

I have the following code in my app delegate as a shortcut for working with CoreData in my other viewControllers:

我的应用程序委托中有以下代码作为在其他 viewController 中使用 CoreData 的快捷方式:

let ad = UIApplication.shared.delegate as! AppDelegate
let context = ad.persistentContainer.viewContext

However, I now get the error message:

但是,我现在收到错误消息:

"UI API called from background thread" and "UIApplication.delegate must be used from main thread only".

“从后台线程调用的 UI API”和“只能从主线程使用 UIApplication.delegate”。

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?

当我的应用程序在后台时,我正在使用 CoreData,但这是我第一次看到此错误消息。有人知道这里发生了什么吗?

Update: I tried to move this inside the appDelegate class itself, and using the following code -

更新:我尝试将其移动到 appDelegate 类本身中,并使用以下代码 -

let dispatch = DispatchQueue.main.async {
    let ad = UIApplication.shared.delegate as! AppDelegate
    let context = ad.persistentContainer.viewContext
}

Now, I can no longer access the ad and context variables outside the AppDelegate. Is there something I'm missing?

现在,我无法再访问AppDelegate. 有什么我想念的吗?

采纳答案by Krunal

With ref to this (-[UIApplication delegate] must be called from main thread only) in Swift (for your query resolution)

在 Swift 中引用此(-[UIApplication delegate] 必须仅从主线程调用)(用于您的查询解析)

    DispatchQueue.main.async(execute: {

      // Handle further UI related operations here....
      //let ad = UIApplication.shared.delegate as! AppDelegate
      //let context = ad.persistentContainer.viewContext   

    })

With edit:(Where is the correct place to declare ad and context? Should I declare these in my viewControllers in the main dispatch)
Place of variables (ad and context) declaration defines scope for it. You need to decide what would be scope of these variable. You can declare them Project or application level (Globally), class level or particular this function level. If you want to use these variable in other ViewControllers then declare it globally or class level with public/open/internal access control.

使用编辑:(声明广告和上下文的正确位置在哪里?我应该在主调度的视图控制器中声明这些)
变量(广告和上下文)声明的位置定义了它的范围。您需要决定这些变量的范围。您可以将它们声明为项目或应用程序级别(全局)、类级别或特定的此功能级别。如果您想在其他 ViewController 中使用这些变量,请使用公共/开放/内部访问控制在全局或类级别声明它。

   var ad: AppDelegate!    //or var ad: AppDelegate?
   var context: NSManagedObjectContext!    //or var context: NSManagedObjectContext?


   DispatchQueue.main.async(execute: {

      // Handle further UI related operations here....
      ad = UIApplication.shared.delegate as! AppDelegate
      context = ad.persistentContainer.viewContext   

      //or 

      //self.ad = UIApplication.shared.delegate as! AppDelegate
      //self.context = ad.persistentContainer.viewContext   

    })