即使使用Dispatch Queue,URLSession也非常慢响应请求

时间:2017-07-24 14:56:26

标签: ios swift urlsession

我正在使用DispatchQueue方法,但我没有遇到更快的响应时间,我称错了吗?

func httpRequest(_ collection: String, httpSuccess: @escaping ((_ data: Data) -> Void), httpFailure:((_ error: NSError) -> Void)?, method: String!, params: String? = nil) {

 ....


let task = session.dataTask(with: request, completionHandler: {data, response, error in
     print("0")      
     if let responseError = error as NSError! {
     print("1")
         DispatchQueue.main.async { print("2"); httpFailure?(responseError) }
     } else if let httpResponse = response as? HTTPURLResponse {
         print("3")
         if httpResponse.statusCode < 200 || httpResponse.statusCode > 300 {
            let statusError = NSError(domain:"domain.com", code:httpResponse.statusCode, userInfo:[NSLocalizedDescriptionKey : "HTTP request returned status \(httpResponse.statusCode)"])
            DispatchQueue.main.async {  httpFailure?(statusError) }
         } else {
            print("5")
            DispatchQueue.main.async { print("6");  httpSuccess(data!) }
         }
     }
 })
 task.resume()
}

0 个答案:

没有答案