Heim > Fragen und Antworten > Hauptteil
NSURLSessionConfiguration中有两个超时设置,一个叫timeoutIntervalForRequest 另一个是timeoutIntervalForResource,这两个有什么不同?一般在何种情况下设置?
给我你的怀抱2017-05-02 09:40:08
下面的这段文字:参考这里
timeoutIntervalForRequest 和 timeoutIntervalForResource指定了请求以及该资源的超时时间间隔。许多开发人员试图使用timeoutInterval去限制发送请求的总时间,但这误会了timeoutInterval的意思:报文之间的时间。timeoutIntervalForResource实际上提供了整体超时的特性,这应该只用于后台传输,而不是用户实际上可能想要等待的任何东西。
自己测试和理解
在一定的情况下(timeoutInterval,timeoutIntervalForResource,timeoutIntervalForRequest)这三个值均可以触发请求超时。
分两种情况理解,即有没有给NSURLReaqust设置timeoutInterval。
1.如果设置了NSURLRequest timeoutInterval:
忽略timeoutIntervalForRequest设置,以timeoutIntervalForResource、timeoutInterval较小的值为超时时间。
2.没有设置NSURLRequest timeoutInterval:
以timeoutIntervalForRequest、timeoutIntervalForResource较小的值为超时时间,并不是Request timeoutInterval的默认60s。
NSURLRequest和SessionConfiguration都不设置使用默认值60s超时。
background session模式下上传下载忽略掉timeoutIntervalForRequest和timeoutInterval的设置
使用timeoutIntervalForResource
NSURLRequest
timeoutInterval-(默认60s)
SessionConfiguration
timeoutIntervalForRequest- (默认60s)
timeoutIntervalForResource- (默认7天)
@property NSTimeInterval timeoutIntervalForRequest;
Description
The timeout interval to use when waiting for additional data.
This property determines the request timeout interval for all tasks within sessions based on this configuration. The request timeout interval controls how long (in seconds) a task should wait for additional data to arrive before giving up. The timer associated with this value is reset whenever new data arrives. When the request timer reaches the specified interval without receiving any new data, it triggers a timeout.
The default value is 60.
Important
Any upload or download tasks created by a background session are automatically retried if the original request fails due to a timeout. To configure how long an upload or download task should be allowed to be retried or transferred, use the timeoutIntervalForResource property.
AvailabilityiOS (7.0 and later), macOS (10.9 and later), tvOS (9.0 and later), watchOS (2.0 and later)
@property NSTimeInterval timeoutIntervalForResource;
Description
The maximum amount of time that a resource request should be allowed to take.
This property determines the resource timeout interval for all tasks within sessions based on this configuration. The resource timeout interval controls how long (in seconds) to wait for an entire resource to transfer before giving up. The resource timer starts when the request is initiated and counts until either the request completes or this timeout interval is reached, whichever comes first.
The default value is 7 days.
AvailabilityiOS (7.0 and later), macOS (10.9 and later), tvOS (9.0 and later), watchOS (2.0 and later)
@property NSTimeInterval timeoutInterval;
Description
The receiver’s timeout interval, in seconds.
If during a connection attempt the request remains idle for longer than the timeout interval, the request is considered to have timed out. The default timeout interval is 60 seconds.
As a general rule, you should not use short timeout intervals. Instead, you should provide an easy way for the user to cancel a long-running operation. For more information, read Designing for Real-World Networks in Networking Overview.
AvailabilityiOS (8.0 and later), macOS (10.10 and later), tvOS (9.0 and later), watchOS (2.0 and later)
正常情况下设置timeoutIntervalForRequest(根据文档我觉得可以理解为请求的默认超时时间)和NSURLRequest timeoutInterval(针对单个请求设置超时时间)
特殊情况下(background session模式下上传、下载)设置timeoutIntervalForResource