0

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

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Ziggy
  • 121
  • 1
  • 9
  • Please refer [this](https://stackoverflow.com/a/39712843/1746086) link, for checking when constraints are updates. – Shah Nilay Aug 12 '17 at 10:39

2 Answers2

1

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 }
Jaydeep Vora
  • 6,085
  • 1
  • 22
  • 40
  • Thank you for answer but maybe I missed something or I explained badly. The buttonGo must remain disabled until it receives the authorization. When the authorization is good then the buttonGo goes enable. I know that the authorization is ok only when the method delegate updateView() is called correctly. – Ziggy Aug 12 '17 at 11:21
  • Thank you, Jaydeep and Bhadresh. Now it seems I solved. Inside the method updateView(), I put your suggestion and now it seems to work well. Thanks also to @Nilay who showed me the right way to get it how to apply the change – Ziggy Aug 13 '17 at 06:28
0

Call your method on main thread.

DispatchQueue.main.async { 
       updateView()
}
Bhadresh Mulsaniya
  • 2,610
  • 1
  • 12
  • 25
  • Thank you for answer but maybe I missed something or I explained badly. The buttonGo must remain disabled until it receives the authorization. When the authorization is good then the buttonGo goes enable. I know that the authorization is good when the method delegate updateView() is called correctly. – Ziggy Aug 12 '17 at 11:16