我最近在尝试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];
}
}
注意 - 我只测试了一个文件,而不是多个文件,但我的目标是下载多个文件..
感谢。