static func downloadByFullURLSync<T: Codable>(url: String, timeoutInterval: Double = 60) -> T? {
CXLogUtil.e("download 0 sync \(Thread.currentThread())")
let semaphore = DispatchSemaphore.init(value: 0)
var responseModel: T?
let start = Date()
Alamofire.download(URLRequest(url: NSURL.init(string: url)! as URL, timeoutInterval: timeoutInterval))
.responseJSON(queue: DispatchQueue.global(qos: DispatchQoS.QoSClass.background), options: .allowFragments, completionHandler: { dataResponse in
CXLogUtil.e("download 2 sync \(Thread.currentThread())")
print("\n\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
print("request url: ", dataResponse.request?.url ?? "")
print("\nrequest headers:\n")
dataResponse.request?.allHTTPHeaderFields?.forEach { key, value in
print("\t", key, value)
}
print("\n<<<<<<<<<<---------->>>>>>>>>>\n")
print("response headers:\n")
dataResponse.response?.allHeaderFields.forEach { key, value in
print("\t", key, value)
}
print("\nresponse body:\n\n\t\(dataResponse.result.value ?? "")\n")
let ms = round(Date().timeIntervalSince(start) * 1000)
print("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 耗时: \(ms) ms \n\n")
if (dataResponse.result.isSuccess && dataResponse.result.value != nil) {
responseModel = CXJsonUtil.parse(T.self, withJSONObject: dataResponse.result.value ?? "")
print("\nresponse success:\n")
} else {
responseModel = nil
let failureMessage = dataResponse.result.error?.localizedDescription.lowercased() ?? "网络错误"
print("\nresponse failure:\n\n\t\(failureMessage)\n")
}
semaphore.signal()
})
_ = semaphore.wait(timeout: DispatchTime.distantFuture)
CXLogUtil.e("download 1 sync \(Thread.currentThread())")
return responseModel
}
[ios] swift 4.0 Alamofile 同步 执行
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- Objective-C 12345678910use_frameworks!platform :ios, "8.0...
- 最近在看Swift 4.0,看到SQLite部分,回想起之前在Swift下使用SQLite很是麻烦(当然OC下也是...