Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating UILabel.text is slow

Tags:

ios

swift

Right now, my view controller is doing some very simple things:

  1. Call a web service
  2. Println a value
  3. Set a UILabel's text value to that value

1 & 2 are pretty snappy. The third step takes about 10 seconds to appear on screen in the simulator.

        switch currentTemp {
    case 80..<180:
        self.riskType.text = "Heat Index:"
        self.perceivedTemperatureValue.text = "\(currentHeatIndex)"
    case -100..<50:
        self.riskType.text = "Wind Chill:"
        self.perceivedTemperatureValue.text = "\(currentWindChill)"
    default:
        self.riskType.text = "Temperature:"
        println(currentTemp)
        self.perceivedTemperatureValue.text = "\(currentTemp)"
    }

Any idea why this is so slow? Is there something else I need to do to make the change appear as soon as I println the value?

like image 571
Mike Pulsifer Avatar asked Mar 19 '23 16:03

Mike Pulsifer


1 Answers

There's not a whole lot to go on in the code you posted, but it sounds like you're doing your label updates directly in your web service callback, which usually runs on a background thread. All UI work needs to be done on the main thread or you'll run in to problems like this. If so, running that switch statement on the main thread using GCD's dispatch_async will fix your problem:

dispatch_async(dispatch_get_main_queue()) {
    switch currentTemp {
    case 80..<180:
        self.riskType.text = "Heat Index:"
        self.perceivedTemperatureValue.text = "\(currentHeatIndex)"
    case -100..<50:
        self.riskType.text = "Wind Chill:"
        self.perceivedTemperatureValue.text = "\(currentWindChill)"
    default:
        self.riskType.text = "Temperature:"
        println(currentTemp)
        self.perceivedTemperatureValue.text = "\(currentTemp)"
    }
}
like image 105
Mike S Avatar answered Mar 29 '23 17:03

Mike S