NSURLConnection + NSMutableData for downloading files is very slow

Basically, what happens, I need to download a whole bunch of files in my application, and I set up a sort queue that loads every file using NSURLConnection and periodically saves the server response in NSMutableData until the download is done and then writes it all to disk.

Here are the relevant parts:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)_response { response = [_response retain]; if([response expectedContentLength] < 1) { data = [[NSMutableData alloc] init]; } else { data = [[NSMutableData dataWithCapacity:[response expectedContentLength]] retain]; } } - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)_data { [data appendData:_data]; } - (void)connectionDidFinishLoading:(NSURLConnection *)connection { NSLog(@"saved: %@", self.savePath); [data writeToFile:self.savePath atomically:YES]; } 

Can anyone understand why this would be terribly slow? This is pretty bad with the simulator and gets worse on the device itself. My maximum download size is about 2 megabytes, so I decided to keep everything in memory until it runs out, it would not be such a bad idea. This at best is around 20 kB / s (with a direct wifi ad-hoc connection).

Edit: in all my test cases, I get a Content-Length header, so this is not a question of increasing NSMutableData with every response received.

Edit 2: this one is all that the Shark gives me. A.

Edit 3: So this is how I made the connection

 NSMutableURLRequest *request = [[NSMutableURLRequest requestWithURL:[NSURL URLWithString:[@"http://xxx.xxx.xxx.xxx/index.php?service=" stringByAppendingString:service]]] retain]; [request setHTTPMethod:@"POST"]; [request setHTTPBody:[[options JSONRepresentation] dataUsingEncoding:NSUTF8StringEncoding]]; NSURLConnection *conn = [NSURLConnection connectionWithRequest:request delegate:self]; [conn start]; 

Of course, in fact, I don't have a hardcoded url, and both requests and conn are instance variables of the loader class. Not that it matters, but for JSON I use http://code.google.com/p/json-framework/ . Parameters and service are method parameters (NSString and NSDictionary), but they should not have a value.

+1
source share
2 answers
The boy is embarrassed. It turns out that my Content-Length header was inaccurate, which caused NSURLConnection to have to wait for some timeout before it would finish, even though it had all the data. Really makes sense. Perhaps this will help someone else.
+2
source

I would have a profile to find out where the slowdown occurs and in which template. Place the log statement in connection:didReceiveData to see how often it is called. You are looking for:

  • Relative elapsed time between method calls.
  • Does the time between calls increase as the application starts?

If the elapsed time between calls is where the application spends most of its time, then the bottleneck is in the request itself. Or the request is not configured correctly on the server, which is not sent quickly.

If the time between calls increases longer than the application is running, then this is probably a memory problem. As data becomes larger and memory becomes more limited, the application must swap more and more memory, which slows everything down. To test, register the various didReciveMemoryWarning methods in any active objects.


Update:

According to Shark, the problem is in your request for the url, not in the code you posted. You need to see how you configured the request.

0
source

All Articles