同步运行嵌套的Alamofire请求

时间:2019-01-20 12:17:08

标签: swift alamofire

我试图通过多次请求从我的节点中检索数据(以获取汽车行驶的距离)。但是,似乎执行执行的顺序不正确。我认为这是由于请求未同步处理造成的。

我看过其他问题,但是由于嵌套的请求,这似乎不适合我的情况。

var d1:加倍!     var d2:Double!     var d3:加倍!     var d4:Double!

var speed: Double!
var speed1: Double!

@objc func getSpeed() {

    Alamofire.request("\(Constants.appServer)/speed", method: .get).responseJSON { (response) in

        if let result = response.result.value as! [String:Any]? {
            if let response = result["data"] as! [String: Any]?
            {
                self.d1 = response["distance"] as? Double
                print("First distance is \(self.d1) KM")
                //Delay for 5 secods
                sleep(5)
                //Make the call again.
                Alamofire.request("\(Constants.appServer)/speed", method: .get).responseJSON(completionHandler: { (response) in

                    if let result = response.result.value as! [String: Any]? {
                        if let response = result["data"] as! [String:Any]?
                        {
                            self.d2 = response["distance"] as? Double
                            print("The second distance is \((self.d2)!)")
                            let distance = self.d2 - self.d1
                            print("The total distance for the first part is \(distance)")
                            self.speed = (distance / 5) * 1000
                            print("\((self.speed)!) is the speed (m/s) for the first part")
                        }
                    }
                })
            }
        }
    }

    sleep(30)
    Alamofire.request("\(Constants.appServer)/speed", method: .get).responseJSON { (response) in

        if let result = response.result.value as! [String:Any]? {
            if let response = result["data"] as! [String: Any]?
            {
                self.d3 = response["distance"] as? Double
                print("The third distance is \((self.d3)!)")
                //Delay for 5 secods
                sleep(5)
                //Make the call again.
                Alamofire.request("\(Constants.appServer)/speed", method: .get).responseJSON(completionHandler: { (response) in

                    if let result = response.result.value as! [String: Any]? {
                        if let response = result["data"] as! [String:Any]?
                        {
                            self.d4 = response["distance"] as? Double
                            print("The fourth distance is \((self.d4)!)")
                            let distance = self.d4 - self.d3
                            print("The total distance for the second part is \(distance)")
                            self.speed = (distance / 5) * 1000
                            print("\((self.speed)!) is the speed (m/s) for the second part")
                        }
                    }})
            }
        }
    }
    if speed1 == 0 && speed > 90 {
        //Wait 5 minutes to see if the car moves
        sleep(30)
        if speed1 == 0 {
            //Send SMS
            Alamofire.request("\(Constants.appServer)/message?long=longitude&lat=lat", method: .get).responseJSON { _ in}
        }
    }else {
        print("There is no issue")
    }
}

我大约每5分钟运行一次以下功能,

0 个答案:

没有答案