1

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];
Neeku
  • 3,646
  • 8
  • 33
  • 43
Romain
  • 151
  • 1
  • 12
  • It sounds more like network bottleneck. How much data does your app upload/download per request? – tia Sep 01 '14 at 15:11

2 Answers2

1

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.

Woodstock
  • 22,184
  • 15
  • 80
  • 118
  • I meant that I want to know if there's a way to reduce time, such as using a socket so it reduces the time to establish connection each times... Stuffs like that – Romain Sep 01 '14 at 15:18
  • Yup, that's exactly what I'm saying - using a consistently connected socket WILL absolutely increase the speed and responsiveness of your network activities. – Woodstock Sep 01 '14 at 15:19
  • Do you have any tips on how to use a socket ? Some examples ;) – Romain Sep 02 '14 at 08:38
0

I've been dealing with this issue for, I can't tell how long! Today I learnt that I can use gzip compression to fasten up things. To use this, you need to have it installed on the server, and the good news is if you're using apache2 on your server, it comes by default. To test to make sure your server/URL had gzip compression enabled, test it with this online tool: http://www.feedthebot.com/tools/gzip/

If the answer is yes, proceed to add the code to your Objective-C code in Xcode. After this line in your code:

NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];

just add this other line:

[theRequest addValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"];

This will speed up your response time noticeably, and you do not need to use AFNetworking for a small NSURLRequest or NSURLConnection task.

Neeku
  • 3,646
  • 8
  • 33
  • 43
  • Thanks man, I'm not working on this project anymore but that's the idea, I compressed the XML and I had better time responses. Though, I don't know how the Twitter app is written, it's way more fast. Is JSON quicker ? – Romain Feb 13 '15 at 08:59
  • Hmm... Good point! I don't know. I had never thought about it and have been so surprised that such a small trick can boost up the speed. But have never tried JSON. However this question here seems to confirm that JSON is faster: http://stackoverflow.com/questions/4596465/is-parsing-json-faster-than-parsing-xml – Neeku Feb 13 '15 at 20:46