- 必須僅從主線程調用[UIApplication委托]

[英]-[UIApplication delegate] must be called from main thread only


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.

這個警告導致一個嚴重的問題,因為我真的無法使用Xcode 9 beta 2在主線程之外調用委托。奇怪的是,當我使用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?

另外我認為只從主線程調用委托只是好習慣,不是嗎?那么為什么這會導致應用程序崩潰呢?

2 个解决方案

#1


35  

Just call it from the main thread like this.

只需從主線程中調用它。

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

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

像這樣與您的應用代表聯系,暗示您的架構可以使用一點清理。

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.

您可以從任何您想要的線程調用代理。你只需要確保你在UIKit調用的主線程上。或者您正在使用CoreData對象所期望的正確線程。這完全取決於您的對象具有的API合約。

#2


2  

In Swift, you could also use DispatchQueue.main.async to call the UI controlling method from the main thread

在Swift中,您還可以使用DispatchQueue.main.async從主線程調用UI控制方法

DispatchQueue.main.async {
    YourUIControlMethod()
}

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2017/07/13/72585d16db4d3796fff63bf78dcfff15.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com