iphone - Why is my NSURLConnection so slow? -
i have setup nsurlconnection using guidelines in using nsurlconnection mac os x reference library, creating nsmutableurlrequest post php script custom body upload 20 mb of data (see code below). note post body (line breaks , all) match existing desktop implementation.
when run in iphone simulator, post successful, takes order of magnitude longer if run equivalent code locally on mac in c++ (20 minutes vs. 20 seconds, respectively).
any ideas why difference dramatic? understand simulator give different results actual device, expect @ least similar results.
thanks
const nsuinteger kdatasizepost = 20971520; 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]; (unsigned int = 0 ; < 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 user connection failed. } [request release]; [uniquedata release];
ok, there's couple of things wrong here.
- you're generating 20mb of random data, take ages on ios device :)
- you're keeping 20mb in memory, little memory intensive on ios device. you'd better off saving file.
- you're setting body manually isn't necessary. i'd recommend using asiformdatarequest , not using nsdata , using asiformdatarequest's addfile:withfilename:andcontenttype:forkey:
Comments
Post a Comment