I am building arrays via a for in loop inside a closure, then passing these arrays through a segue. I am able to print the contents of the arrays from within the closure, but when I access the array from outside, it is always empty.
I have tried to use .reloadData() to update the arrays outside of the closure, but I can't seem to get it quite right. Does anyone know if this is the correct approach?
Here is my code:
var distances: [Double]
var journeyTimes: [Double]
directions.calculate { response, error in
if let route = response?.routes.first {
print("Distance: \(route.distance/1000) km, ETA: \(route.expectedTravelTime/60) mins")
self.distances.append(route.distance/1000)
self.journeyTimes.append(route.expectedTravelTime/60)
print(self.distances)
print(self.journeyTimes)
} else {
print("Error!")
} updateArrays()
}
func updateArrays() {
self.collectionView.reloadData()
}
The print statements above produce results, and my prepare for segue carries distances and journeyTimes through, but they are always empty when I try to access them.
I think I need to update the distances and journeyTimes arrays before prepare for segue, outside of the closure, but I don't know how to do it.
Grateful for any help on this
Thanks