I have this view with a couple of labels and a map. In this view I'm displaying some information about a geo-location point:
- The address
- The map + the pin
- A first label (calculated)
- A second label (calculated)
Once the view is loading I need to do some calculation to update the labels. This calculation are taking few seconds (I need to call an API) so I've put them in a queue:
dispatch_async(dispatch_get_main_queue(), {
let loc = CLLocationCoordinate2D(latitude: self.place!.location.latitude, longitude: self.place!.location.longitude)
let tmp = Int(Geo.apiCall(self.currentPosition, coordTo: loc));
self.label1.text = " = \(tmp) unit";
})
I've used the main thread dispatch_get_main_queue()
because I need to update the label.
The problem is that it's blocking the rendering of the map and it's also blocking the other async function from CLGeocoder
that is gathering the address from the geo-location point.
let loc = CLLocation(latitude: self.place!.location.latitude, longitude: self.place!.location.longitude)
CLGeocoder().reverseGeocodeLocation(loc, completionHandler:
{(placemarks, error) in
if error != nil {
println("reverse geodcode fail: \(error.localizedDescription)")
}
let pms = placemarks as [CLPlacemark]
if pms.count > 0 {
let pm = placemarks[0] as CLPlacemark
self.address.text = ABCreateStringWithAddressDictionary(pm.addressDictionary, false)
}
})
So the view is displayed correctly, but all the label are updated at the same time, few seconds after the view is displayed, and the map is loading only when the label are rendered.
What would be the best work around to avoid this blocking when the view is rendering?