Zoltan Zoltan - 1 year ago 57
iOS Question

After HTTP POST request UIView change delays

I have use this code to make HTTP POST request:

let myURL = NSURL(string: serverURL)

let request = NSMutableURLRequest(URL: myURL!)

request.HTTPMethod = "POST"

request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)

let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
data, response, error in

if error != nil {
} else {
let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
self.view.backgroundColor = UIColor.redColor()



I have experience that the view's
changes after several seconds from the print of the responseString.

Why is that and how can I make it simultaniously?


Answer Source

Always perform UI changes on main thread, so change your view's backgrondColor on the main thread like this way.

    self.view.backgroundColor = UIColor.redColor()
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download