Closing Using an Asynchronous Method (Swift 2.0)

I am working on a project that calculates ETA between different coordinates of MapView. I use the asynchronous method calculateETAWithCompletionHandlerto get the ETA between two coordinates, and since it calculateETAWithCompletionHandleris asynchronous, my code is not linear.

I need my code to be linear in order to display the correct ETA information in the TableView, so I tried to implement a close to return in an asynchronous call. However, this has not yet led to the fact that my code was linear. The following is what I still have

override func viewDidLoad() {
    var etaBetween1n2 = 0

    let point1 = MKPointAnnotaion()
    point1.coordinate = CLLocationCoordinate2D(latitude: 36.977317, longitude: -122.054255)
    point1.title = "Point 1"
    mapView.addAnnotation(point1)

    let point2 = MKPointAnnotaion()
    point2.coordinate = CLLocationCoordinate2D(latitude: 36.992781, longitude: -122.064729)
    point2.title = "Point 2"
    mapView.addAnnotation(point2)

    print("A")

    // Closure
    calculateETA(point1, destination: point2) { (eta: Int) -> Void in
        print("B")
        etaBetween1n2 = eta
    }

    print("C")

}

// Calculates ETA between source and destination
// Makes calculateETAWithCompletionHandler call which is asynchronous
func calculateETA(source: MKPointAnnotation, destination: MKPointAnnotation, result: (eta: Int) -> Void) {
    var eta = 0

    let request = MKDirectionsRequest()
    let sourceItem = MKMapItem(placemark: MKPlacemark(coordinate: source.coordinate, addressDictionary: nil))
    request.source = sourceItem
    request.transportType = .Automobile

    let destinationItem = MKMapItem(placemark: MKPlacemark(coordinate: destination.coordinate, addressDictionary: nil))
    request.destination = destinationItem

    request.destination = destinationItem
    request.requestsAlternateRoutes = false
    let directions = MKDirections(request: request)

    directions.calculateETAWithCompletionHandler { (etaResponse, error) -> Void in
        if let error = error {
            print("Error while requesting ETA : \(error.localizedDescription)")
        } else {
            eta = Int((etaResponse?.expectedTravelTime)!)
            result(eta: eta)
        }
    }

}

I was hoping that closing would make my code a linear way to print,

A
B
C

but instead he prints,

A
C
B

Am I implementing closure incorrectly or is closing the wrong approach for this?

+4
2

, , , , , , .

, - , , , ( "B" )

-, , , , .

?

+1

, "C" B, :

print("A")
calculateETA(point1, destination: point2) { (eta: Int) -> Void in
    print("B")
    etaBetween1n2 = eta

    print("C")
}

"C" , , , . .

    doStepA()
    calculateETA(point1, destination: point2) { (eta: Int) -> Void in
        doStepB()
        etaBetween1n2 = eta

        doStepC()
    }
}

func doStepA() {
    print("A")
}

func doStepB() {
    print("B")
}

func doStepC() {
    print("C")
}
+1

Source: https://habr.com/ru/post/1628563/


All Articles