'OUTDATED: Updating a UILabel to display current location
TLDR;
I want to get locations outside the main thread using a different function but I am receiving delays.
In details:
I'm performing a GET request. later on I parse the JSON to get the current locaction in a method called 'locationManager' now I have a UILabel outlet that I want to update all the time to the current location displayed on the map according to the result of the JSON (does reverse geocode) but if I access the label.text through the locationManager I have a delay + Xcode tells me that it should only be done through the main thread.
so I've tried making a global variable and updating it in the locationManager but then when I access the label.text in viewDidLoad() and feed it the global variable it stays empty (initial value)
what could be a possible fix to this?
var addrs = "" // the global variable
...
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
//Code
addrs = result.response.view[0].result[0].location.address.label
} .resume()
override func viewDidLoad() {
super.viewDidLoad()
locationLabel.text = ("Your Current Location Is :" + addrs)
}
Solution 1:[1]
Note: Always update your UI's only on the main thread. Use below code to resolve your issue.
DispatchQueue.main.async { [weak self] in
addrs = result.response.view[0].result[0].location.address.label
self?.locationLabel.text = ("Your Current Location Is :" + addrs)
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Yervand Saribekyan |