Why is my NSURLConnection so slow?

Posted by Bama91 on Stack Overflow See other posts from Stack Overflow or by Bama91
Published on 2011-02-09T15:22:20Z Indexed on 2011/02/09 15:25 UTC
Read the original article Hit count: 226

Filed under:
|
|

I have setup an NSURLConnection using the guidelines in Using NSURLConnection from the Mac OS X Reference Library, creating an NSMutableURLRequest as POST to a PHP script with a custom body to upload 20 MB of data (see code below). Note that the post body is what it is (line breaks and all) to exactly match an existing desktop implementation.

When I run this in the iPhone simulator, the post is successful, but takes an order of magnitude longer than if I run the equivalent code locally on my Mac in C++ (20 minutes vs. 20 seconds, respectively).

Any ideas why the difference is so dramatic? I understand that the simulator will give different results than the actual device, but I would expect at least similar results.

Thanks

const NSUInteger kDataSizePOST = 20971520;
const NSString* kServerBDC = @"WWW.SOMEURL.COM";
const NSString* kUploadURL = @"http://WWW.SOMEURL.COM/php/test/upload.php";
const NSString* kUploadFilename = @"test.data";
const NSString* kUsername = @"testuser";
const NSString* kHostname = @"testhost";

srandom(time(NULL));
NSMutableData *uniqueData = [[NSMutableData alloc] initWithCapacity:kDataSizePOST];
for (unsigned int i = 0 ; i < kDataSizePOST ; ++i) {
    u_int32_t randomByte = ((random() % 95) + 32);
    [uniqueData appendBytes:(void*)&randomByte length:1];
}

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:kUploadURL]];
[request setHTTPMethod:@"POST"];

NSString *boundary = @"aBcDd";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];

NSMutableData *postbody = [NSMutableData data];
[postbody appendData:[[NSString stringWithFormat:@"--%@\nContent-Size:%d",boundary,[uniqueData length]] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"\nContent-Disposition: form-data; name=test; filename=%@", kUploadFilename] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithString:@";\nContent-Type: multipart/mixed;\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

[postbody appendData:[NSData dataWithData:uniqueData]];

[postbody appendData:[[NSString stringWithFormat:@"--%@\nContent-Size:%d",boundary,[kUsername length]] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"\nContent-Disposition: inline; name=Username;\n\r\n%@",kUsername] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"--%@\nContent-Size:%d",boundary,[kHostname length]] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"\nContent-Disposition: inline; name=Hostname;\n\r\n%@",kHostname] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"\n--%@--",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

[request setHTTPBody:postbody];

NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (theConnection) {
    _receivedData = [[NSMutableData data] retain];
} else {
    // Inform the user that the connection failed.
}

[request release];
[uniqueData release];

© Stack Overflow or respective owner

Related posts about iphone

Related posts about nsurlconnection