0

We use a PHP server and an iOS app.

I use NSURLConnection to send a message to the server. If server is unavailable, e.g. the address is incorrect then we wait for an error after 60–90 secs. This is a long time.

How do I decrease this time? Is there any quick function to check for server availability?

If I first call

[request setTimeoutInterval:10];
[request setHTTPBody:[message data]];

when I check [request timeoutInterval] it is still 240, not 10. So interesting..

If I add a setTimeoutInterval: call before the connection is created like so:

[request setTimeoutInterval:10];
connection = [[NSURLConnection alloc] initWithRequest: request delegate: self];

The request still returns after a long time.

Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
CReaTuS
  • 2,593
  • 1
  • 19
  • 31
  • make Synchronous request : NSData *data = [NSURLConnection sendSynchronousRequest:<#(NSURLRequest *)#> returningResponse:<#(NSURLResponse *__autoreleasing *)#> error:<#(NSError *__autoreleasing *)#>]; if data is not available server is offline – Paresh Navadiya May 21 '12 at 06:07
  • this is a true code? or just sample without syntax? – CReaTuS May 21 '12 at 08:36
  • it is true : NSData *responsedata = [NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http:www.google.com"]] returningResponse:nil error:nil]; – Paresh Navadiya May 21 '12 at 08:43
  • Still request is came after long time – CReaTuS May 21 '12 at 09:38
  • How are you initializing `request`? – kevboh May 21 '12 at 11:25

4 Answers4

1

If the Server really is down, you could always first check with the reachabillity Framework and check if the server can be reached, before you fire your HTTP request.

See this answer for a sample of this:

Reachability Guide for iOS 4

Community
  • 1
  • 1
Maffo
  • 1,445
  • 1
  • 14
  • 17
1

Yeah, timeouts don't quite work like you would expect. One solution is to set up a timer that cancels the connection after your desired interval has elapsed. I did this in my ReallyRandom class, and it seems to do the trick.

It looks something like this:

connection = [[NSURLConnection alloc] initWithRequest: request delegate: self startImmediately:NO];

timer = [NSTimer scheduledTimerWithTimeInterval:10
                                     target:self
                                   selector:@selector(onTimeExpired)
                                   userInfo:nil
                                    repeats:NO];

[connection start];


// When time expires, cancel the connection
-(void)onTimeExpired
{
    [self.connection cancel];
}
Marty
  • 7,464
  • 1
  • 31
  • 52
0

You can reduce the timeout time in the request, specifying the timeoutInterval parameter in the method:

+ (id)requestWithURL:(NSURL *)theURL cachePolicy:(NSURLRequestCachePolicy)cachePolicy timeoutInterval:(NSTimeInterval)timeoutInterval

But it's difficult to tell whether the server is unavailable or just busy. You can just decide that if the server does not respond in a certain interval, it is likely that it is down. Otherwise you will have to wait.

Fran Sevillano
  • 8,103
  • 4
  • 31
  • 45
  • We use code [request setTimeoutInterval: 10], but after call, the time will have value 240. So interesting – CReaTuS May 21 '12 at 08:35
0

This Delegate

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error


if (error.code == NSURLErrorTimedOut)
jose920405
  • 7,982
  • 6
  • 45
  • 71