0

我正在使用 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()
}
4

0 回答 0