ios - How can I speed up my request to enabled a Bar Button Item in Navigation Item?


Keywords:ios 


Question: 

In first ViewController in ViewDidLoad(), there are 2 operations:

  1. Puts buttonGo.isEnabled = false (buttonGo is Bar Button Item)
  2. Makes a request, through a web service to check if the device is authorized.

To check if the device is authorized is done inside a singleton class delegate. It calls: DownloadAuthorize that has got a delegate method: updateView().

If the device is authorize, I will call back the delegate method updateView() in ViewController and this method makes buttonGo.isEnabled = true

Everything works well, but the buttonGo goes enabled after few seconds. I investigate in this way and it seems to me that the buttonGo goes to enabled by the background thread. 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"

Is it possible to do something to speed up the controls to refresh?

Thank you


2 Answers: 

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. 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.

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

Call your method on main thread.

DispatchQueue.main.async { 
       updateView()
}