簡體   English   中英

為什么我的NSURLConnection這么慢?

[英]Why is my NSURLConnection so slow?

我已使用 Mac OS X參考庫中的“ 使用NSURLConnection”中的指南設置了NSURLConnection,並使用自定義主體將NSMutableURLRequest創建為POST到PHP腳本,以上傳20 MB數據(請參見下面的代碼)。 請注意,帖子正文是與現有桌面實現完全匹配的內容(換行和全部)。

當我在iPhone模擬器中運行此代碼時,發布成功,但是比我在Mac上以C ++在Mac上本地運行等效代碼的時間長了一個數量級(分別為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];

好的,這里有些錯誤。

  • 您正在生成20MB的隨機數據,這會在iOS設備上使用一段時間:)
  • 您要保留20MB的內存,這在iOS設備上會占用很少的內存。 您最好將其保存到文件中。
  • 您還可以手動設置主體,這是不必要的。 我建議您使用ASIFormDataRequest ,甚至不使用NSData和ASIFormDataRequest的addFile:withFileName:andContentType:forKey:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM