简体   繁体   English

如何加快在导航项中启用“条形按钮”项的请求?

[英]How can I speed up my request to enabled a Bar Button Item in Navigation Item?

In first ViewController in ViewDidLoad(), there are 2 operations: 在ViewDidLoad()的第一个ViewController中,有2个操作:

  1. Puts buttonGo.isEnabled = false (buttonGo is Bar Button Item) 放置buttonGo.isEnabled = false(buttonGo是条形按钮项目)
  2. Makes a request, through a web service to check if the device is authorized. 通过Web服务发出请求,以检查设备是否被授权。

To check if the device is authorized is done inside a singleton class delegate. 在单例类委托中检查设备是否被授权。 It calls: DownloadAuthorize that has got a delegate method: updateView(). 它调用:DownloadAuthorize,它具有一个委托方法:updateView()。

If the device is authorize, I will call back the delegate method updateView() in ViewController and this method makes buttonGo.isEnabled = true 如果设备被授权,我将在ViewController中回调委托方法updateView(),此方法使buttonGo.isEnabled = true

Everything works well, but the buttonGo goes enabled after few seconds. 一切正常,但是buttonGo几秒钟后启用。 I investigate in this way and it seems to me that the buttonGo goes to enabled by the background thread. 我以这种方式进行调查,在我看来,buttonGo由后台线程启用。 I tried to use view.setNeedsLayout(), view.setNeedsDisplay() in delegate method, because I hoped to speed up the redraw component but I get such mistakes similar of this: "This application is modifying the autolayout engine from a background thread after the engine was accessed from the main thread. This can lead to engine corruption and weird crashes" 我尝试在委托方法中使用view.setNeedsLayout(),view.setNeedsDisplay(),因为我希望加快重绘组件的速度,但是却遇到类似的错误:“此应用程序正在从后台线程修改自动布局引擎从主线程访问引擎。这可能导致引擎损坏和奇怪的崩溃”

Is it possible to do something to speed up the controls to refresh? 是否可以做一些事情来加快控件的刷新速度?

Thank you 谢谢

It's done obviously, because it's the code which is modifying the AutoLayout UI elements/constraints from the code which is either running in Background thread or completion handlers. 显然,这样做是因为代码正在修改在后台线程或完成处理程序中运行的代码中的AutoLayout UI元素/约束。 All completion handlers by default run in background thread. 默认情况下,所有完成处理程序都在后台线程中运行。 You need to use the GCD to update the UI elements from your completion handler blocks. 您需要使用GCD从完成处理程序块中更新UI元素。

DispatchQueue.main.async { // UI update/changes task here }

Call your method on main thread. 在主线程上调用您的方法。

DispatchQueue.main.async { 
       updateView()
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM