如果超时请求用完,我正在尝试设置网络会话创建者返回上一个屏幕。截至目前,我不完全确定在哪里或如何执行它。这是代码:
lazy var configuration: URLSessionConfiguration = URLSessionConfiguration.default
lazy var session: URLSession = URLSession(configuration: self.configuration)
typealias JSONData = ((Data) -> Void)
func getJSONData(type: String, urlExtension: String, completion: @escaping JSONData) {
configuration.timeoutIntervalForRequest = 5
configuration.timeoutIntervalForResource = 5
let request = URLRequest(url: URL(string:"\(baseURL)\(type)/\(urlExtension)?api_key=\(apiKey)")! )
let dataTask = session.dataTask(with: request, completionHandler: { (data, response, error) in
if error == nil {
if let httpResponse = response as? HTTPURLResponse {
switch (httpResponse.statusCode) {
case 200:
if let data = data {
completion(data)
}
default:
print(httpResponse.statusCode)
}
}
} else {
print("Error: \(error?.localizedDescription)")
}
})
dataTask.resume()
}