What is the difference between timeoutIntervalForRequest timeoutIntervalForResource
PHPz2017-05-02 09:38:08
0
1
1826
There are two timeout settings in NSURLSessionConfiguration, one is called timeoutIntervalForRequest and the other is timeoutIntervalForResource. What is the difference between the two? Under what circumstances is it generally set?
The following text: refer to here timeoutIntervalForRequest and timeoutIntervalForResource specify the timeout interval for the request and the resource. Many developers try to use timeoutInterval to limit the total time to send a request, but this misunderstands the meaning of timeoutInterval: the time between messages. timeoutIntervalForResource actually provides the feature of an overall timeout, which should only be used for background transfers, not anything the user might actually want to wait for.
Test and understand yourself
Under certain circumstances (timeoutInterval, timeoutIntervalForResource, timeoutIntervalForRequest) these three values can trigger request timeout.
It can be understood in two situations, that is, whether timeoutInterval is set for NSURLReaqust.
1. If NSURLRequest timeoutInterval is set: Ignore the timeoutIntervalForRequest setting, and use the smaller value of timeoutIntervalForResource and timeoutInterval as the timeout time.
2. NSURLRequest timeoutInterval is not set: The smaller value of timeoutIntervalForRequest and timeoutIntervalForResource is the timeout time, which is not the default 60s of Request timeoutInterval.
NSURLRequest and SessionConfiguration are not set to use the default value of 60s timeout.
Upload and download in background session mode ignore the settings of timeoutIntervalForRequest and timeoutInterval Use timeoutIntervalForResource
@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)
The following text: refer to here
timeoutIntervalForRequest and timeoutIntervalForResource specify the timeout interval for the request and the resource. Many developers try to use timeoutInterval to limit the total time to send a request, but this misunderstands the meaning of timeoutInterval: the time between messages. timeoutIntervalForResource actually provides the feature of an overall timeout, which should only be used for background transfers, not anything the user might actually want to wait for.
Test and understand yourself
Under certain circumstances (timeoutInterval, timeoutIntervalForResource, timeoutIntervalForRequest) these three values can trigger request timeout.
It can be understood in two situations, that is, whether timeoutInterval is set for NSURLReaqust.
1. If NSURLRequest timeoutInterval is set:
Ignore the timeoutIntervalForRequest setting, and use the smaller value of timeoutIntervalForResource and timeoutInterval as the timeout time.
2. NSURLRequest timeoutInterval is not set:
The smaller value of timeoutIntervalForRequest and timeoutIntervalForResource is the timeout time, which is not the default 60s of Request timeoutInterval.
NSURLRequest and SessionConfiguration are not set to use the default value of 60s timeout.
Upload and download in background session mode ignore the settings of timeoutIntervalForRequest and timeoutInterval
Use timeoutIntervalForResource
NSURLRequest
timeoutInterval-(default 60s)
SessionConfiguration
timeoutIntervalForRequest- (default 60s)
timeoutIntervalForResource- (default 7 days)
@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