URLSession dataTask 长延时
URLSession dataTask long delay
当我从我的代码中请求 API 时,我在 4-6 秒后得到了响应,这太长了。我在 120 毫秒后收到邮递员的回复。我的代码中有什么地方出错了吗?
这是我的代码,我正在检查两次打印之间的时间:
func makeUrlRequest<T: Codable>(_ request: URLRequest, resultHandler: @escaping (Result<T, RequestError>) -> Void) {
var req = request
req.addValue("application/json", forHTTPHeaderField: "Content-Type")
req.addValue("application/json", forHTTPHeaderField: "Accept")
let config = URLSessionConfiguration.default
let urlSession = URLSession(configuration: config, delegate: self, delegateQueue: .main)
print("Request: start at: \(Date())") //Request: start at: 2021-04-09 06:53:32 +0000
let urlTask = urlSession.dataTask(with: req) { data, response, error in
print("Request: finished at: \(Date())") //Request: finished at: 2021-04-09 06:53:36 +0000
DispatchQueue.main.async {
guard error == nil else {
resultHandler(.failure(.clientError))
return
}
guard let response = response as? HTTPURLResponse, 200...299 ~= response.statusCode else {
resultHandler(.failure(.serverError))
return
}
guard let data = data else {
resultHandler(.failure(.noData))
return
}
guard let decodedData: T = self.decodedData(data) else {
resultHandler(.failure(.dataDecodingError))
return
}
resultHandler(.success(decodedData))
}
}
urlTask.resume()
}
而不是
let urlSession = URLSession(configuration: config, delegate: self, delegateQueue: .main)
print("Request: start at: \(Date())") //Request: start at: 2021-04-09 06:53:32 +0000
let urlTask = urlSession.dataTask(with: req) { data, response, error in...
你应该使用
let task = URLSession.shared.dataTask(with: req) { (data, response, error) in...
编辑:
我了解到您想使用委托。在这种情况下,您不应使用完成处理程序,而应添加委托方法:
func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data)
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?)
查看 Apple 的 documentation 了解更多信息。
当我从我的代码中请求 API 时,我在 4-6 秒后得到了响应,这太长了。我在 120 毫秒后收到邮递员的回复。我的代码中有什么地方出错了吗? 这是我的代码,我正在检查两次打印之间的时间:
func makeUrlRequest<T: Codable>(_ request: URLRequest, resultHandler: @escaping (Result<T, RequestError>) -> Void) {
var req = request
req.addValue("application/json", forHTTPHeaderField: "Content-Type")
req.addValue("application/json", forHTTPHeaderField: "Accept")
let config = URLSessionConfiguration.default
let urlSession = URLSession(configuration: config, delegate: self, delegateQueue: .main)
print("Request: start at: \(Date())") //Request: start at: 2021-04-09 06:53:32 +0000
let urlTask = urlSession.dataTask(with: req) { data, response, error in
print("Request: finished at: \(Date())") //Request: finished at: 2021-04-09 06:53:36 +0000
DispatchQueue.main.async {
guard error == nil else {
resultHandler(.failure(.clientError))
return
}
guard let response = response as? HTTPURLResponse, 200...299 ~= response.statusCode else {
resultHandler(.failure(.serverError))
return
}
guard let data = data else {
resultHandler(.failure(.noData))
return
}
guard let decodedData: T = self.decodedData(data) else {
resultHandler(.failure(.dataDecodingError))
return
}
resultHandler(.success(decodedData))
}
}
urlTask.resume()
}
而不是
let urlSession = URLSession(configuration: config, delegate: self, delegateQueue: .main)
print("Request: start at: \(Date())") //Request: start at: 2021-04-09 06:53:32 +0000
let urlTask = urlSession.dataTask(with: req) { data, response, error in...
你应该使用
let task = URLSession.shared.dataTask(with: req) { (data, response, error) in...
编辑:
我了解到您想使用委托。在这种情况下,您不应使用完成处理程序,而应添加委托方法:
func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data)
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?)
查看 Apple 的 documentation 了解更多信息。