objective-c和php 的time差了八个小时,objective-c - timeoutIntervalForRequest timeoutIntervalForResource 有什么区...

给我你的怀抱2017-05-02 09:40:081楼

下面的这段文字:参考这里

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值