I am making requests for distance calculation from my current location to campsite locations in an array countySites
using the calculate()
method of the MKDirections
class in a for
loop. When I have an array of length 47 campsites only some of the campsite distances are calculated, the remainder produce an error message suggesting too many requests are being made. I don't know how to check that the calculate()
isn't too busy before I make a request in the for
loop.
I have tried placing a while
loop in the code to repeatedly check for a calculation still being processed using the isCalculating
Bool
property but it fails to stop the error messages.
countySites = england.counties[selectedCounty!]!
let sourcePlacemark = MKPlacemark(coordinate: currentCoordinate!)
let request = MKDirections.Request()
request.source = MKMapItem(placemark: sourcePlacemark)
request.transportType = .automobile
request.requestsAlternateRoutes = false
let group = DispatchGroup()
activityIndicator.startAnimating()
// Step through sites one by one
for siteIndex in 0..<countySites.count {
var selectedSite = countySites[siteIndex]
let destinationPlacemark = MKPlacemark(coordinate: selectedSite.locationCoordinate)
request.destination = MKMapItem(placemark: destinationPlacemark)
let distanceAndDirections = MKDirections(request: request)
if currentCoordinate != nil {
group.enter()
distanceAndDirections.calculate { (response, error) in
if error == nil {
let distanceInMetres = response?.routes.first?.distance
let distanceInMiles = distanceInMetres! / 1610
let roundedDistanceInMiles = Int(distanceInMiles.rounded())
let distanceToSite = roundedDistanceInMiles
selectedSite.distance = distanceToSite
self.countySites[siteIndex] = selectedSite
} else {
print(error.debugDescription)
}
group.leave()
}
}
}
group.notify(queue: .main) {
self.countySites = self.sortByDistance(sites: self.countySites)
self.tableView.reloadData()
self.activityIndicator.stopAnimating()
}
}
}