Swift3 Swift 3,在使用URLSession从服务器加载映像时要执行超时
我用的是swift 3。我想在执行下载任务时向URLSession添加超时。我确实使用了配置来更改我的设置,但是,它不起作用。代码没有执行超时。。。如果服务器没有快速响应,它将失败 这是我的密码:Swift3 Swift 3,在使用URLSession从服务器加载映像时要执行超时,swift3,timeout,nsurlsessiondownloadtask,urlsession,Swift3,Timeout,Nsurlsessiondownloadtask,Urlsession,我用的是swift 3。我想在执行下载任务时向URLSession添加超时。我确实使用了配置来更改我的设置,但是,它不起作用。代码没有执行超时。。。如果服务器没有快速响应,它将失败 这是我的密码: import Foundation import UIKit extension UIImageView { func loadImage(url: URL) -> URLSessionDownloadTask { let session: URLSession = {
import Foundation
import UIKit
extension UIImageView {
func loadImage(url: URL) -> URLSessionDownloadTask {
let session: URLSession = {
let configuration = URLSessionConfiguration.default
configuration.timeoutIntervalForRequest = 70
configuration.timeoutIntervalForResource = 70
return URLSession(configuration: configuration, delegate: nil, delegateQueue: nil)
}()
let downloadTask = session.downloadTask(with: url, completionHandler: { [weak self] url, response, error in
if error == nil, let url = url, let data = try? Data(contentsOf: url), let image = UIImage(data: data) {
DispatchQueue.main.async {
if let strongSelf = self {
strongSelf.image = image
}
}
}
})
downloadTask.resume()
return downloadTask
}
}
任何评论都将不胜感激 var timeoutitervalforresource:TimeInterval{get set}Discussion此属性根据此配置确定会话中所有任务的资源超时间隔。资源超时时间间隔控制在放弃之前等待整个资源传输的时间(秒)。资源计时器在启动请求时启动,并计数,直到请求完成或达到此超时时间间隔,以先到者为准。默认值为7天。请尝试注释掉TimeOutiterValforResource。默认值为7天,您将其设置为70秒