Joe Licari Joe Licari - 10 months ago 153
iOS Question

NSURLSessionConfiguration timeoutIntervalForRequest vs NSURLSession timeoutInterval

With the new NSURLSession, there is now a timeoutIntervalForRequest on the NSURLSessionConfiguration object used to create the session.

But there is still a timeoutInterval on the NSURLRequest object that can be used to create the NSURLSessionTask in the session.

My question is, if the configuration has a timeoutIntervalForRequest set to 30, but the NSURLRequest use to create the task has a timeoutInterval of 60, which timeout interval will actually be used?

Answer Source

As my investigation on iOS 7.0.3, timeoutInterval for NSURLRequest does not get any effects when it is used with NSURLSession.

Whether you set timeoutIntervalForRequest for NSURLSessionConfiguration or not, timeoutInterval is just ignored.

You can use my tiny sample application NetworkTimeoutSample for checking this behavior.

When you set 1 for 'URLReq' field, which affects timeoutInterval for NSURLRequest, then click 'NSURLSession With URLRequest' button, your session will not get timeout error.

You also may recognize you should set timeoutIntervalForResource, instead of timeoutIntervalForRequest for NSURLSession if you'd like to get same timeout effect for timeoutInterval of NSURLRequest.

If you set both timeoutIntervalForRequest and timeoutIntervalForResource value for NSURLSessionConfiguration, smaller value will be affected(I feel this behavior is different from current API document).

There are no documentation about those specifications, so it might be changed on future iOS version.