是否可以使用一个URLSession
为每个URLRequest
设置不同的超时间隔?
我尝试设置
urlRequest.timeoutInterval = 20 * 60
但它已被 URLSession 忽略,并且请求在默认 60 秒后失败并出现超时错误。
只有此设置更改超时:
let configuration = URLSessionConfiguration.default
configuration.timeoutIntervalForRequest = 20 * 60 // 20 min
self.session = URLSession(configuration: configuration)
但是 20 * 60//20 分钟
应用于该 session 中的所有 URLRequest
。
我想让某些任务等待更长的时间来响应,但其余任务应该在默认 60 秒后默认失败。
有可能吗?或者我应该使用多个 URLSession
最佳答案
您可以为每个单独的 URL 配置超时。只是您不能设置大于 URLSession 配置本身的超时。
In some cases, the policies defined in this configuration may be overridden by policies specified by an
NSURLRequest
object provided for a task. Any policy specified on the request object is respected unless the session’s policy is more restrictive. For example, if the session configuration specifies that cellular networking should not be allowed, theNSURLRequest
object cannot request cellular networking.Docs on URLSessionConfiguration
默认超时为 60 秒。您设置为更大的 1200。所以这是行不通的。增加您的配置,然后您可以对每个 url 的超时进行不同的配置。
关于ios - 一个 URLSession 中的不同超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52649471/