Reputation: 1609
In my viewDidLoad I have this method which turns a location from Parse into a city and state. The only problem is, is that it appends in the wrong order most of the time. Also the .location I have on the end of the location variable is a custom method that turns a PFGeoPoint into a CLLocation.
for post in posts {
let location = ((post["locations"] as! NSMutableArray)[0] as! PFGeoPoint).location
CLGeocoder().reverseGeocodeLocation(location()) { (placemarks, error) -> Void in
if error != nil {
print(error)
} else {
let p = CLPlacemark(placemark: placemarks![0])
let locationString = "\(p.locality!), \(p.administrativeArea!)"
self.locationStrings.append(locationString)
}
}
}
I believe this is all happening due to the CLGeoCoder completion handler takes a while to fully execute. I think this could be solved by doing all of this without the completion hander, but I'm not sure how to do that. Thank you!
Upvotes: 0
Views: 81
Reputation: 15331
Reverse geocoding needs to be done asynchronously to prevent lag. You can create an object to hold your post location data, or your post data in general, with a location string property. Instantiate the post object and add them to an array then iterate over them to retrieve the location string and set it on the object. For example:
for postObject in postObjects {
CLGeocoder().reverseGeocodeLocation(postObject.location) { (placemarks, error) -> Void in
if error != nil {
print(error)
} else {
let p = CLPlacemark(placemark: placemarks![0])
postObject.locationString = "\(p.locality!), \(p.administrativeArea!)"
}
}
}
Upvotes: 1