UILabel.text update is slow
At the moment my view controller is doing very simple things:
- Calling a web service
- Print value
- Set text value UILabel to this value
1 and 2 are pretty fast. The third step takes about 10 seconds to appear on the 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 changes as soon as I print the value?
+3
source to share
1 answer
There isn't much in the code you posted, but it looks like you are updating your labels directly in the web service callback, which usually runs on a background thread. All UI work must be done on the main thread, or you will run into problems like this. If so, running this statement switch
on the main thread using GCD is the dispatch_async
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)"
}
}
+7
source to share