Juan Alejandro Galvis
Juan Alejandro Galvis

Reputation: 51

Synchronous request using Alamofire

I have checked other solutions like How to make a synchronous request using Alamofire?, I followed all instructions but I haven't been able to complete the requests and then use the information.

I need to retrieve distance and route points from google maps and then complete an array with that information. This is the function:

func getGoogleMapsInfo(startLocation: CLLocationCoordinate2D, restaurant: Customer, completion: @escaping (_ distance: Int, _ routePoints: String)  -> ()){

    let endLocation = CLLocationCoordinate2D(latitude: restaurant.latitude, longitude: restaurant.longitude)
    let origin = "\(startLocation.latitude),\(startLocation.longitude)"
    let destination = "\(endLocation.latitude),\(endLocation.longitude)"
    var distance = 0
    var routePoints = ""

    let url = "https://maps.googleapis.com/maps/api/directions/json?origin=\(origin)&destination=\(destination)&mode=walking"
    Alamofire.request(url).responseJSON { response in

        switch response.result {
        case .success:

            do {
                self.json = try JSON(data: response.data!)

                distance = Int(truncating: self.json["routes"][0]["legs"][0]["distance"]["value"].numberValue)


                let route = self.json["routes"].arrayValue.first
                let routeOverviewPolyline = route!["overview_polyline"].dictionary
                routePoints = (routeOverviewPolyline?["points"]?.stringValue)!
                completion(distance, routePoints)
                break
            } catch {
                print("error JSON")
            }


        case .failure(let error):
            print(error)
            completion(distance, routePoints)
            break
        }

    }


}

And this is how I call it:

    for index in 0...nearbyRestaurants.count - 1 {

        getGoogleMapsInfo(startLocation: currentLocation, restaurant: nearbyRestaurants[index]) { (distance, routePoints) in
            nearbyRestaurants[index].distance = distance
            nearbyRestaurants[index].routePoints = routePoints
        }

    } 

I'd really appreciate if someone can help me.

Upvotes: 1

Views: 2243

Answers (1)

vadian
vadian

Reputation: 285250

Don't try to make an asynchronous request synchronous

Instead use DispatchGroup,notify is called when all network requests are completed.

let group = DispatchGroup()
for index in 0..<nearbyRestaurants.count {
    group.enter()
    getGoogleMapsInfo(startLocation: currentLocation, restaurant: nearbyRestaurants[index]) { (distance, routePoints) in
        nearbyRestaurants[index].distance = distance
        nearbyRestaurants[index].routePoints = routePoints
        group.leave()
    }
}
group.notify(queue: DispatchQueue.main) {
        print("all info data has been received")
} 

Upvotes: 4

Related Questions