Updating UI as the result of a request handler

201 Views Asked by At

I have a setup like this;

startup() {
    ...
    self.gcdWebServer.addHandlerForMethod("GET", path: "/hide", 
        requestClass: GCDWebServerRequest.self, asyncProcessBlock: {request in self.hide()})
    ...
}

func hide() -> GCDWebServerDataResponse {
    self.view.hidden = true;
    print("hide")
    return GCDWebServerDataResponse(statusCode: 200)
}

When a request to /hide is made, the console shows the print() call immediately, but the view does not disappear for some arbitrary delay, somewhere between 10-30 seconds.

How can I have the request immediately result in the view being hidden?

4

There are 4 best solutions below

0
On BEST ANSWER

Try this one, calling hidden on main thread.

dispatch_async(dispatch_get_main_queue(),{
   self.view.hidden = true;
})
0
On

Wrap you r UI related login inside dispatch async and run it on main thread:

dispatch_async(dispatch_get_main_queue(),{

    self.view.hidden = true;

 })
0
On

Rewrite your hide method as below. You need update UI on main thread only.

func hide() -> GCDWebServerDataResponse {
    dispatch_async(dispatch_get_main_queue(),{
        self.view.hidden = true
    })
    print("hide")
    return GCDWebServerDataResponse(statusCode: 200)
}
0
On

UI update code write in main thread only.

 dispatch_async(dispatch_get_main_queue(),{

        self.view.hidden = true;

     })