Search code examples
iosxmlnsurlconnection

NSURLConnection too slow


I am using an NSURLConnection in my app where I scan a barcode, send the XML via the NSURLConnection and the java service sends me back an XML. My problem here is that with Wifi, the response time is decent, but when I use 3G, it's freaking slow.

How can I manage to fasten things up in 3G?

  NSString *xmlHomeMade = [NSString stringWithFormat:
                         @"<?xml version='1.0'?>"
                         "<service name='CorePDAService' method='blahblah'>"
                         "<arg class='java.lang.String'>%@</arg>"
                         "</service>",cab];


NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

NSString *ipAgence = [defaults objectForKey:@"ipAgence"];

NSLog(@"agence IP Test : %@",ipAgence);


NSURL *url = [NSURL URLWithString:ipAgence];


NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@"%d", [xmlHomeMade length]];

[theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setTimeoutInterval:10];
[theRequest setHTTPBody: [xmlHomeMade dataUsingEncoding:NSUTF8StringEncoding]];


NSError *error = nil;
NSURLResponse *urlResponse = nil;
NSData* data = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&urlResponse error:&error] ;


if(urlResponse == nil){

    if (error) {

        [self displayAlert:@"" message:@"Error"];

    }

}


NSString* newStr = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];


[self parseXML:data];

Solution

  • The answer here is implicit.

    3G is a cellular technology which suffers a lot more from network nasties like Jitter & Latency than your WiFi connection (which is probably ADSL, VDSL or cable).

    In particular (due to the way 3G is implemented) when it begins to create a connection it has to request network resources and allocate a channel (it's a lot more complicated than that - but it's basically the jist, I used to be a developer for a telecoms hardware provider).

    The only real way to "speed" up a 3G connection is to either a) slim down the payload or b) keep an active connection (radio access barer) consistently.

    3G as a technology is high latency and bursty. One you begin a burst it usually operates at reasonable pace (relatively) but when creating the connection initially there is some serious latency.

    When the connection is inactive for a period of time it will drop the resources it was granted by the radio network controllers, and re-requesting this is part of the slow down.