问题描述
我正在使用 iOS 7 的新 NSURLSessionDataTask
来检索数据,如下所示:
I am using iOS 7's new NSURLSessionDataTask
to retrieve data as follows:
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:
request completionHandler:
^(NSData *data, NSURLResponse *response, NSError *error) {
//
}];
如何增加超时值以避免错误请求超时"
(在NSURLErrorDomain
Code=-1001
)?
How can I increase the time out values to avoid the error "The request timed out"
(in NSURLErrorDomain
Code=-1001
)?
我已经检查了 NSURLSessionConfiguration 但没有找到设置超时值的方法.
I have checked the documentation for NSURLSessionConfiguration but did not find a way to set the time out value.
感谢您的帮助!
推荐答案
ObjC
NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
sessionConfig.timeoutIntervalForRequest = 30.0;
sessionConfig.timeoutIntervalForResource = 60.0;
迅捷
let sessionConfig = URLSessionConfiguration.default
sessionConfig.timeoutIntervalForRequest = 30.0
sessionConfig.timeoutIntervalForResource = 60.0
let session = URLSession(configuration: sessionConfig)
文档说了什么
timeoutIntervalForRequest
和 timeoutIntervalForResource
指定请求和资源的超时间隔.
What docs say
timeoutIntervalForRequest
and timeoutIntervalForResource
specify the timeout interval for the request as well as the resource.
timeoutIntervalForRequest
- 等待时使用的超时间隔用于附加数据.与此值关联的计时器被重置每当新数据到达时.当请求定时器达到指定的时间间隔没有收到任何新数据,它会触发一个超时.
timeoutIntervalForResource
- 一个应该允许资源请求.此值控制如何在放弃之前等待整个资源转移的时间很长.这资源计时器在请求发起时启动并计数直到请求完成或达到此超时间隔,以先到者为准.
timeoutIntervalForResource
- The maximum amount of time that a resource request should be allowed to take. This value controls how long 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.
基于 NSURLSessionConfiguration 类参考
这篇关于NSURLSession:如何增加 URL 请求的超时时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!