-[UIApplication 委托] 只能从主线程调用 [英] -[UIApplication delegate] must be called from main thread only

查看:32
本文介绍了-[UIApplication 委托] 只能从主线程调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个警告导致了一个严重的问题,因为我真的无法使用 Xcode 9 beta 2 在主线程之外调用委托.奇怪的是,当我使用 Xcode 8.3.3 时,这是有效的.

This warning leads to a serious problem cause I really can't call the delegate outside of the main thread using Xcode 9 beta 2. Strange thing is that this was working when I was using Xcode 8.3.3.

我还认为只从主线程调用委托是一种很好的做法,不是吗?那么为什么这会导致应用程序崩溃?

Also I thought it would only be good practice to call delegates from main thread only, isn't it? So why is this causing the app to crash now?

推荐答案

就这样从主线程调用即可.

Just call it from the main thread like this.

目标 C

dispatch_async(dispatch_get_main_queue(), ^{
  [[UIApplication delegate] fooBar];
});

迅捷

DispatchQueue.main.async {
  YourUIControlMethod()
}

像这样联系您的应用委托,暗示您的架构可以进行一些清理.

Reaching out to your app delegate like this, is a hint that your architecture could use a little cleanup.

你可以从任何你想要的线程调用委托.您只需要确保您在 UIKit 调用的主线程上.或者您在 CoreData 对象期望的正确线程上.这完全取决于您的对象拥有的 API 合同.

You can call delegates from any thread you want. You only need to make sure you're on the main thread for UIKit calls. Or that you're on the correct thread your CoreData objects expect. It all depends on the API contract your objects have.

这篇关于-[UIApplication 委托] 只能从主线程调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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