为什么我的NSURLConnection这么慢?

时间:2011-02-09 15:22:21

标签: iphone nsurlconnection http-post

我已经使用Mac OS X参考库中Using NSURLConnection中的指南设置了NSURLConnection,创建了一个NSMutableURLRequest作为POST脚本,使用自定义正文来上传20 MB数据(参见下面的代码)。请注意,帖子正文就是它(换行符和所有)与现有桌面实现完全匹配。

当我在iPhone模拟器中运行时,帖子成功,但比我在Mac上用C ++本地运行等效代码(分别为20分钟对20秒)要长一个数量级。

为什么差异如此戏剧性的任何想法?我知道模拟器将提供与实际设备不同的结果,但我预计至少会有类似的结果。

由于

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

1 个答案:

答案 0 :(得分:1)

好的,这里有一些错误。

  • 您正在生成20MB的随机数据,这在iOS设备上需要很长时间:)
  • 你在内存中保留了20MB,这对iOS设备来说有点内存密集。你最好把它保存到文件中。
  • 你也在手动设置身体,这是不必要的。我建议使用ASIFormDataRequest,甚至不使用NSData并使用ASIFormDataRequest的addFile:withFileName:andContentType:forKey: