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

Deadly 提交于 2019-11-26 08:22:58

问题


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?


回答1:


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.




回答2:


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

DispatchQueue.main.async {
    YourUIControlMethod()
}


来源:https://stackoverflow.com/questions/45081731/uiapplication-delegate-must-be-called-from-main-thread-only

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