NSMutableURLRequest setTimeoutInterval not working in ios 11.0
Asked Answered
C

4

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

Conflict answered 23/11, 2017 at 8:0 Comment(14)
You can pass timeout directly to your session configuration like session.timeoutIntervalForRequest = 30.0; session.timeoutIntervalForResource = 60.0;Mahalia
See this https://mcmap.net/q/234524/-nsurlsession-how-to-increase-time-out-for-url-requestsMahalia
already tried backgroundSession.timeoutIntervalForRequest = 300.0f; backgroundSession.timeoutIntervalForResource = 300.0f;Conflict
As per my thinking Timeout does not have any relation with internet connection unavailability , Since time out should only works when internet is working and mean time request couldn't finished !!Mahalia
You can check rateability and on connection lost event you can either pause your request with resume data and re start when all is wellMahalia
timeout is getting even if I do not provide timeout interval.Conflict
According to developer.apple.com/documentation/foundation/… The default value is 60.Mahalia
I am getting issue in ios 11.0.1Conflict
Let us continue this discussion in chat.Conflict
Possibly a dumb question, but have you tried reseting your simulator (reset content and settings) and/or verifying that the network connectivity is using a single source? i.e. strictly WIFIBobo
hi @Bobo I am not verifying any network connectivity, what would you do if net connection is slow and API getting timeout within 10 seconds. 10 seconds is not enough. Try running my ViewController.m demo file you will understand yourself. Run it using 9.0 and then run inside 11.0Conflict
@Warewolf the VC file is a .m without the header/storyboard functions, so please add it to a git repository w/ link.Bobo
@Bobo just call IBAction method through a button.Conflict
https://mcmap.net/q/323444/-nsmutableurlrequest-not-obeying-my-timeoutinterval refer this link i hope it should help you @ WarewolfWarlock
A
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;
            },.....
Anthologize answered 28/11, 2017 at 11:42 Comment(5)
@Warewolf i have set NSTimeInterval reqTimeInterval = 10.0f; then also its working and giving output.Anthologize
I am also able to get response in few seconds, probably you didn't understand my question yet. Turn off your net connection then hit API. My question was why it is not waiting for more than 10 seconds.Conflict
i have turn off net connection and then hit api. it will take 60 seconds by default even i give 30 seconds.Anthologize
which ios you are trying on?Conflict
Let us continue this discussion in chat.Conflict
D
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.

Disturbing answered 27/11, 2017 at 7:56 Comment(1)
I have already tried everything I could including your suggestion, If you have ios 11.0 then test on it by turning off network connectivity.Conflict
M
0

Set timeoutIntervalForRequest property for the backgroundSession.

Missioner answered 27/11, 2017 at 12:38 Comment(1)
check Agula Otgonbaatar answer, is it different from that ?Conflict
D
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:

Dialecticism answered 29/11, 2017 at 10:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.