7

With my code below, the failure block fires immediately if there is no internet connection - and that is good. But what if there is a connection, but no internet?

I've read this question: How to set a timeout with AFNetworking which suggests to use the reachabilityManager and the example in this answer shows the use - AFNetworking 2.0 queue request when device is offline with setReachabilityStatusChangeBlock does nothing.

BUT, if my simulator or phone is connected to my wifi network but does not have internet access (DNS, DHCP, or modem issues), my code at present continues to try to reach my API for a long time. I'm not downloading anything and I know my script and my server should respond in seconds so I know that after 5 seconds of inactivity, something is wrong.

So can I safely do a timeout, or can I use the reachabilityManager in my current script to detect if the script (not the internet) is not reachable and if so, how?

- (void)APICall:(NSMutableDictionary*)params {

    NSURL *baseURL = [NSURL URLWithString:BaseURLString];
    NSDictionary *parametersGetAuthCode = @{@"req": @"getauth"};

    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:baseURL];

    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    [manager POST:APIscript parameters:parametersGetAuthCode success:^(NSURLSessionDataTask *task, id responseObject) {
        if ([task.response isKindOfClass:[NSHTTPURLResponse class]]) {
            NSHTTPURLResponse *r = (NSHTTPURLResponse *)task.response;
            if ([r statusCode] == 200) {

            //do success stuff

            }
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [alertView show];

        //do failure stuff

    }];

}
Community
  • 1
  • 1
Warren
  • 1,984
  • 3
  • 29
  • 60
  • 1
    "I know my script and my server should respond in seconds" > You can never make such an assumption on a mobile network... 3G / Edge can be real slow – Vincent Guerci Jun 24 '14 at 11:46
  • Go under a bridge, go into old bars under big construction, and you may have a really slow data connection. So you'll block the access to your users? – Larme Jun 24 '14 at 12:22
  • I know I'm on the wrong side of bad practice, but I would rather my users get a timeout then experience abnormally long attempts to communicate with the server. – Warren Jun 25 '14 at 10:55

2 Answers2

20

Just figure out what a reasonable timeout interval is for your app and set it on the AFHTTPSessionManager Request Serializer.

Objective-C

[manager.requestSerializer setTimeoutInterval:20.0];  

Swift

manager.requestSerializer.timeoutInterval = 20

Add something to your UI, like a UIActivityIndicatorView or a "Downloading..." message so the user knows that something is happening.

Vladimir Amiorkov
  • 2,652
  • 3
  • 17
  • 35
race_carr
  • 1,387
  • 12
  • 21
1

In Swift you can use the below code to set timeout.

manager.requestSerializer.timeoutInterval = 25
Sudhi 9135
  • 745
  • 8
  • 17