NSURLConnection + NSMutableData文件下载真的很慢 [英] NSURLConnection + NSMutableData for file downloads is really slow

查看:112
本文介绍了NSURLConnection + NSMutableData文件下载真的很慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上发生的是,我需要在我的应用程序下载一大堆文件,我设置了一个排序队列,下载每个文件与NSURLConnection并存储服务器响应增量在NSMutableData直到下载

Basically what's happening is that I need to download a whole bunch of files in my app and I've set up a queue of sorts that downloads each file with an NSURLConnection and stores the server response incrementally in an NSMutableData until the download is finished and then writes the whole thing to disk.

以下是相关的部分:

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

有关为什么会非常慢的任何见解?这是相当糟糕的模拟器,并在实际设备上变得更糟。我的最大下载大小是大约2兆字节,所以我想把存储在内存中的整个事情,直到它完成不会是一个坏主意。这最多可达到大约20KB / s(使用直接ad-hoc无线连接)。

Any insights as to why this would be awfully slow? It's pretty bad with the Simulator and gets even worse on an actual device. My maximum download size is around 2 megabytes, so I figured storing the whole thing in memory until it finishes wouldn't be that bad of an idea. This gets up to about 20KB/s at best (with a direct ad-hoc wifi connection).

编辑:在我所有的测试用例中,我得到一个Content-Length头部,所以不是每增加一个NSMutableData接收到响应的比特。

in all my test cases I do get a Content-Length header, so it's not a matter of growing the NSMutableData with each bit of response received.

编辑2:都是鲨鱼给我的。

Edit 2: this is all Shark gives me.

编辑3:这是我如何设置连接

Edit 3: So this is how I set up 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];

当然,我实际上没有硬编码的url,并且请求和conn都是实例变量下载类。不是说它应该重要,但对于JSON,我使用 http://code.google。 com / p / json-framework / 。选项和服务是方法参数(NSString和NSDictionary),而不是那些应该是重要的。

Of course I don't actually have a hardcoded url and both request and conn are instance variables of the downloader class. Not that it should matter, but for JSON I'm using http://code.google.com/p/json-framework/. Options and service are method parameters (NSString and NSDictionary), not that those should matter either.

推荐答案

男孩这是尴尬。结果我的Content-Length头是不准确的,这导致NSURLConnection需要等待某种超时,在它将完成之前,即使它有所有的数据。真的有意义。也许这会帮助别人出去。

Boy this is embarrassing. Turns out my Content-Length header was inaccurate, which resulted in NSURLConnection needing to wait for some sort of timeout before it would finish, even though it had all the data. Makes sense really. Maybe this will help someone else out.

这篇关于NSURLConnection + NSMutableData文件下载真的很慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆