我希望提交一个请求来从API获取信息。 API正在快速响应。下面你可以看到会话数据任务需要5秒钟来响应,这使得应用程序无法缓慢。这是为了观察。我错过了什么吗?
func httpRequest(_ collection: String, httpSuccess: @escaping ((_ data: Data) -> Void), httpFailure:((_ error: NSError) -> Void)?, method: String!, params: String? = nil) {
....
print(" -- ")
printTime(string: "before request")
let task = URLSession.shared.dataTask(with: request, completionHandler: {(data, response, error) -> Void in
DispatchQueue.main.async {
self.printTime(string: "after request")
httpSuccess(data!)
}
})
task.resume()
}
当上面的代码运行时,我会在打印控制台窗口中看到它。
--
before request: 14:23:10
after request: 14:23:15
--
before request: 14:23:15
after request: 14:23:20