尝试启动dataTask时出现URLError(_nsError:操作无法完成)错误

时间:2019-02-10 21:05:24

标签: swift nsurlsession vapor urlsession

我有一个Vapor服务器应用程序(托管在Heroku上),该应用程序通过URLSession dataTask连接到外部API服务。

到目前为止,它一直运行良好(并且在Mac上本地运行时仍然可以正常工作),但是自从上次服务器重新启动以来,dataTask每次都会引发以下错误:URLError(_nsError: The operation could not be completed)

我听说Linux上的Swift过去曾经在URLSession上遇到过问题,但是大多数都可以解决。有人对此有任何解决方案吗?谢谢!

代码:

func foo(task: RepeatedTask) {

        // get request to ZTM API
        guard let client = try? app.client() else {
            return
        }
        var urlComponents = URLComponents(string: Globals.API_URL)!
        urlComponents.queryItems = [
            URLQueryItem(name: "resource_id", value: Globals.RESOURCE_ID),
            URLQueryItem(name: "apikey", value: Globals.API_KEY),
            URLQueryItem(name: "type", value: "1")]
        let url = urlComponents.url
        let response = client.get(url!)


        let tempModel = response.flatMap(to: Model.self) { response -> EventLoopFuture<Model> in
            return try response.content.decode(Model.self)
            }.map(to: Model.self) { tempModel -> Model in
                tempModel.busArray.forEach {
                    $0.vehicleID = $0.Lines! + $0.Brigade!
                    $0.angle = 0
                }
                if Globals.myModel.busArray.isEmpty {
                    Globals.myModel.busArray = tempModel.busArray
                } else {
                    // add & remove buses
                    Globals.myModel.busArray.updateVehicleArray(using: tempModel.busArray)

                    // update coordinates + angle
                    Globals.myModel.busArray.updateCoordinates(using: tempModel.busArray)
                }
                return tempModel
        }
}

urlComponents生成的网址是https://api.um.warszawa.pl/api/action/busestrams_get/?resource_id=f2e5503e-927d-4ad3-9500-4ab9e55deb59&apikey=a86c68be-0117-4a88-838c-bcc2f6ba85f4&type=1

0 个答案:

没有答案