简体   繁体   中英

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];

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM