Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSMutableURLRequest setTimeoutInterval not working in ios 11.0

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

like image 361
Prince Kumar Sharma Avatar asked Nov 23 '17 08:11

Prince Kumar Sharma


1 Answers

@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;
            },.....
like image 53
iNiravKotecha Avatar answered Sep 18 '22 22:09

iNiravKotecha