NSMutableURLRequest setTimeoutInterval not working in ios 11.0

I am using a background URL session to download a PDF as a data stream using ios version> 9.0. I set the timeout interval to 300 seconds. This does not work at all. After 10 seconds, he receives a timeout error.

Below is a snippet of code

NSTimeInterval reqTimeInterval = 300.0f; - (NSURLSession *)uploadSessionForMrNo:(NSString *)mrNo userRoleId:(NSString *)userRoleId timestamp:(NSString *)timestamp { NSOperationQueue *queue = [[NSOperationQueue alloc] init]; queue.maxConcurrentOperationCount = 4; NSString *backgroundSessionIdentifier = [NSString stringWithFormat:@"backgroundPdfUploadIdentifier_%@",mrNo]; NSURLSessionConfiguration *backgroundSession = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:backgroundSessionIdentifier]; backgroundSession.discretionary = true; NSURLSession *session = [NSURLSession sessionWithConfiguration:backgroundSession delegate:self delegateQueue:queue]; [session setAccessibilityLabel:mrNo]; [session setAccessibilityValue:userRoleId]; [session setAccessibilityHint:timestamp]; return session; } - (void)uploadPdfRequest:(NSURLRequest *)request forMrNo:(NSString *)mrNo userRoleId:(NSString *)userRoleId andTimestamp:(NSString *)timestamp { NSURLSession *session = [self uploadSessionForMrNo:mrNo userRoleId:userRoleId timestamp:timestamp]; NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request]; NSLog(@"postDataTask %@ timeout %f",postDataTask,request.timeoutInterval); [postDataTask resume]; } 

Request to load data stream.

 NSMutableURLRequest *request = [[NSURLRequest requestForPDFStringUpload:uploadQueue.uploadData] mutableCopy]; [request setValue:[DataExchange authToken] forHTTPHeaderField:FBENCRYPT_TOKEN_KEY]; [request setCachePolicy:NSURLRequestUseProtocolCachePolicy]; [request setTimeoutInterval:reqTimeInterval]; [self uploadPdfRequest:request forMrNo:uploadQueue.mrNo userRoleId:uploadQueue.userRoleId andTimestamp:uploadQueue.timestamp]; 

delegate

 - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error { if (error) { // Handle error NSLog(@"Error %@",error); } [session finishTasksAndInvalidate]; self.receivedData = nil; } 

If the Internet works fine, then its fine otherwise. After 10 seconds I get

Error Domain Error = NSURLErrorDomain Code = -1001 "Request timed out." UserInfo = {NSErrorFailingURLStringKey = http://test.mydomain.com/common.svc/json/FileUploadPDF , NSErrorFailingURLKey = http://test.mydomain.com/common.svc/json/FileUploadPDF , _kCFStreamErrorEomrorKomrkomkreamkomkrkmekreamkr 2104, NSLocalizedDescription = Request timeout.}

Learn more about this issue.

As I checked my code in the ios 9.3 simulator, it is waiting for a connection. Then the download continues. I tested it for 4 minutes of waiting for its work. But when I run this code before ios 11.0.1 , it gets a timeout after 10 seconds. What do I need to do to achieve this. I also tried

 if ([backgroundSession respondsToSelector:@selector(setWaitsForConnectivity:)]) { [backgroundSession setWaitsForConnectivity:true]; } 

but it does not affect.


You can use the demo file below: -

ViewController.h and .m

+8
objective-c ios11 nsurlsession nsurlsessiondatatask
source share
4 answers

@Warewolf I uploaded your .m file and integrated into my demo project.

everything seems to work in Xcode 9.1 and iOS 11.1

I can get an answer in a few seconds.

Below is your code.

 response of background session --- { RestResponse = { messages = ( "Total [249] records found." ); result = ( { "alpha2_code" = AF; "alpha3_code" = AFG; name = Afghanistan; }, { "alpha2_code" = AX; "alpha3_code" = ALA; name = "\Ufffd\Ufffdland Islands"; }, { "alpha2_code" = AL; "alpha3_code" = ALB; name = Albania; },..... 
+3
source share

I think you should set the value of backgroundSession.timeoutIntervalForResource more than reqTimeInterval.

 NSURLSessionConfiguration *backgroundSession = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:backgroundSessionIdentifier]; backgroundSession.timeoutIntervalForRequest = reqTimeInterval; backgroundSession.timeoutIntervalForResource = reqTimeInterval; 

Hope this helps you.

0
source share

Set the timeoutIntervalForRequest value for backgroundSession.

0
source share

Case 1: Perhaps I helped connect to the local server and received this error. I used a different network in my device and phone. When I connected to the same Wi-Fi, it worked.

Case 2: I encountered an error with code = -1001 "Request timed out." I have tried several things. Nothing succeeded. Finally, I found out that the problem in my parameters is that I am sending the request body to a server that was in the wrong format (the value was fine, but the type was wrong). This is why the request was timed out because the server was unable to process it. You can check this if nothing works for u.

If you do not solve the problem above, try using the GCD abbreviation called [NSURLConnection sendAsynchronousRequest:queue:completionHandler:

0
source share

All Articles