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.