我最近在尝试ASINetworkQueue,在这个过程中我遇到了一个很奇怪的问题。
我尝试从大约 30 MB 大小的服务器下载一个 zip,当我使用简单的 ASIHttpRequest 下载文件时,文件仅在 2 分钟内下载完毕,但是当我尝试将该请求放入 ASINetworkQueue 时,下载时间出人意料地增加了 5 分钟,太奇怪了..如果有人能说有什么不正确的话,我会放代码片段。
[self setNetworkQueue:[ASINetworkQueue queue]];
[[self networkQueue] setDelegate:self];
[[self networkQueue] setShouldCancelAllRequestsOnFailure:NO];
[[self networkQueue] setMaxConcurrentOperationCount:1];
[[self networkQueue] setShowAccurateProgress:YES];
if (!progressBar)
{
progressBar = [[UIProgressView alloc] init];
}
[[self networkQueue] setDownloadProgressDelegate:progressBar];
[[self networkQueue] setRequestDidFinishSelector:@selector(requestFinished:)];
[[self networkQueue] setRequestDidFailSelector:@selector(requestFailed:)];
[[self networkQueue] setQueueDidFinishSelector:@selector(queueFinished:)];
NSArray *arrFilenames = [[NSArray alloc] initWithObjects:@"Audio", nil];
for (NSString *strFilename in arrFilenames)
{
NSString *strDestFolderName = @"Destination Path";
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath: [NSString stringWithFormat:@"%@.zip", strDestFolderName]];
if(!fileExists)
{
NSURL *url = <File Url to be downloaded>;
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
// This file has part of the download in it already
[request setTemporaryFileDownloadPath:[NSString stringWithFormat:@"%@.zip.download", strDestFolderName]];
[request setAllowResumeForFileDownloads:YES];
[request setDownloadDestinationPath:[NSString stringWithFormat:@"%@.zip", strDestFolderName]];
[request setTimeOutSeconds:30];
[request setShouldContinueWhenAppEntersBackground:YES];
[request setNumberOfTimesToRetryOnTimeout:2];
request.userInfo = [NSDictionary dictionaryWithObjectsAndKeys: strFilename, @"fileName", nil];
[[self networkQueue] addOperation:request];
}
}
[[self networkQueue] go];
通过使用上面的代码,下载 Audio.zip 大约需要 5 分钟。但是当我执行以下操作时,需要 2 分钟。
NSArray *arrFilenames = [[NSArray alloc] initWithObjects:@"Audio", nil];
for (NSString *strFilename in arrFilenames)
{
NSString *strDestFolderName = @"Destination Path";
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath: [NSString stringWithFormat:@"%@.zip", strDestFolderName]];
if(!fileExists)
{
NSURL *url = <File Url to be downloaded>;
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
// This file has part of the download in it already
[request setTemporaryFileDownloadPath:[NSString stringWithFormat:@"%@.zip.download", strDestFolderName]];
[request setAllowResumeForFileDownloads:YES];
[request setDownloadDestinationPath:[NSString stringWithFormat:@"%@.zip", strDestFolderName]];
[request setTimeOutSeconds:30];
[request setShouldContinueWhenAppEntersBackground:YES];
[request setNumberOfTimesToRetryOnTimeout:2];
request.userInfo = [NSDictionary dictionaryWithObjectsAndKeys: strFilename, @"fileName", nil];
[request setDidFinishSelector:@selector(requestFinished:)];
[request setDidFailSelector:@selector(requestFailed:)];
[request setDownloadProgressDelegate:progressBar];
[request start];
}
}
注意 - 我只测试一个文件,而不是多个文件,但我的目标是下载多个文件..
谢谢。