UIApplication.delegate只能在主线程中使用 [英] UIApplication.delegate must be used from main thread only

查看:4916
本文介绍了UIApplication.delegate只能在主线程中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的app delegate中有以下代码作为在我的其他viewControllers中使用CoreData的快捷方式:

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

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

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

However, I now get the error message:


从后台线程调用UI API和UIApplication.delegate必须仅从主线程使用。

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

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

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?

更新:我试图在appDelegate类本身内移动它,并使用以下代码 -

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

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

现在,我无法再访问 AppDelegate 之外的广告和上下文变量。有什么东西我不见了吗?

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

推荐答案

参考这个( - [UIApplication delegate]必须仅在主线程中调用)在Swift中(对于您而言)查询解析)

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

    DispatchQueue.main.async(execute: {

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

    })

使用编辑: (声明广告和背景的正确位置在哪里?我应该在主调度中的viewControllers中声明这些吗?

变量位置(广告和上下文)声明定义它的范围。您需要确定这些变量的范围。您可以将它们声明为项目或应用程序级别(全局),类级别或特定此功能级别。
如果要在其他ViewControllers中使用这些变量,则使用公共/开放/内部访问控制将其声明为全局或类级别。

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.

   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   

    })

这篇关于UIApplication.delegate只能在主线程中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆