8

I am using URL background session for pdf uploading as data stream, using ios version > 9.0. I am setting timeout interval of 300 sec. Its not working at all. After 10 seconds it gets timeout error.

Piece of code is given below

  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 upload 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 internet is working fine then its fine otherwise After 10 seconds I get

Error Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." UserInfo={NSErrorFailingURLStringKey=http://test.mydomain.com/common.svc/json/FileUploadPDF, NSErrorFailingURLKey=http://test.mydomain.com/common.svc/json/FileUploadPDF, _kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=-2104, NSLocalizedDescription=The request timed out.}

More detail about this issue,

As I have checked my code in ios 9.3 simulator, it waits for connectivity to appear. Then continues to uploading. I have checked it by 4 minutes of waiting it works. But when I run this code to ios 11.0.1 it gets timeout after 10 seconds. What should I need to do extra to achieve it. I have also tried

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

but it has no effect.


You can use demo file below:-

ViewController.h & .m

Prince Kumar Sharma
  • 12,591
  • 4
  • 59
  • 90

4 Answers4

3

@Warewolf i have downloaded your .m file and integrated in my demo project.

it seems like working all in Xcode 9.1 and iOS 11.1

i am able to get response in few seconds..

here below is your code's output..

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;
            },.....
Nirav Kotecha
  • 2,493
  • 1
  • 12
  • 26
0

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 will help you.

0

Set timeoutIntervalForRequest property for the backgroundSession.

Anuj
  • 820
  • 4
  • 11
0

Case 1: Might be help i was calling local server connection and was getting this error. I was using different network in my device and phone. When I connected both to same wifi, it worked.

Case 2: I had got the error with Code=-1001 “The request timed out.”. I tried a few things. Nothing worked. Finally I found out that the problem was in my parameters that I'm sending in the request body to the server which were of wrong format(value was fine but type was wrong). Thats why the request was timing out coz the server wasn't able to process it. You can check that up if nothing else works for u.

Incase you have not solve problem above case then try using GCDs abstraction called [NSURLConnection sendAsynchronousRequest:queue:completionHandler:

Community
  • 1
  • 1
BuLB JoBs
  • 841
  • 4
  • 20