我不确定我哪里出错了。我必须背靠背下载文件,即,当我触发新的下载时。第一次下载后,应始终进行第二次下载。我必须使用NSOperationQueues来实现这一目标。
我正在使用maxConcurrentOperationCount = 1
,假设下载将连续发生。但是当我看到日志时,下载同时发生。
第二,第三个请求正在执行,而第一个请求下载正在发生。
如何在FIFO中实现执行顺序?
这是代码和日志。任何帮助都非常感谢。提前谢谢。
代码:
#import "ViewController.h"
#import "AFNetworking.h"
//#define DOCUMENT_DIRECTORY_PATH [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]
#define DOCUMENT_DIRECTORY_URL [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil]
#define CACHE_DIRECTORY_PATH [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]
#define URL_DOWNLOAD_ZIP_6KB @"https://codeload.github.com/H2CO3/HCDownload/zip/master"
#define URL_DOWNLOAD_ZIP_37KB @"https://s3.amazonaws.com/hayageek/downloads/ios/apns.zip"
#define URL_DOWNLOAD_ZIP_633KB @"http://cdn1.raywenderlich.com/wp-content/uploads/2015/09/VacationSpots_Complete.zip"
#define URL_DOWNLOAD_ZIP_686KB @"http://cdn3.raywenderlich.com/wp-content/uploads/2014/01/Weather_Final.zip"
#define URL_DOWNLOAD_ZIP_215KB @"http://cdn4.raywenderlich.com/wp-content/uploads/2014/01/GooglyPuff_Start_1.zip"
@interface ViewController () {
int counter ;
NSOperationQueue *zipFileDownloadQueue ;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
counter = 0 ;
zipFileDownloadQueue = [[NSOperationQueue alloc] init] ;
zipFileDownloadQueue.maxConcurrentOperationCount = 1 ;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)downloadBtnTapped:(id)sender {
// NSLog(@"Download button tapped: %d", counter) ;
NSURL *url ;
if (counter == 5) {
counter = 0;
}
++counter ;
if (counter == 1) {
url = [NSURL URLWithString:URL_DOWNLOAD_ZIP_6KB];
} else if (counter == 2) {
url = [NSURL URLWithString:URL_DOWNLOAD_ZIP_633KB];
} else if (counter == 3) {
url = [NSURL URLWithString:URL_DOWNLOAD_ZIP_686KB];
} else if (counter == 4) {
url = [NSURL URLWithString:URL_DOWNLOAD_ZIP_215KB];
} else if (counter == 5) {
url = [NSURL URLWithString:URL_DOWNLOAD_ZIP_37KB];
}
// #1
NSInvocationOperation *fileDownloadOperation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(startDownloadingZipFileAtUrl:) object:url] ;
[zipFileDownloadQueue addOperation:fileDownloadOperation] ;
}
- (void)startDownloadingZipFileAtUrl:(NSURL *)url {
NSLog(@"Downloading file : %@", [url lastPathComponent]) ;
AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager] ;
//for progress
[sessionManager setDownloadTaskDidWriteDataBlock:^(NSURLSession * _Nonnull session, NSURLSessionDownloadTask * _Nonnull downloadTask, int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite) {
float progress = (((float)totalBytesWritten) / totalBytesExpectedToWrite) * 100;
NSLog(@"File :%@ Progress: %f percent", [url lastPathComponent], progress) ;
}] ;
//For downloading
NSURLSessionTask *downloadZipFile =
[sessionManager downloadTaskWithRequest:[NSURLRequest requestWithURL:url]
progress:nil
destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
return [DOCUMENT_DIRECTORY_URL URLByAppendingPathComponent:[response suggestedFilename]];
} completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
NSLog(@"Downloaded file path : %@", filePath) ;
}] ;
[downloadZipFile resume] ;
}
@end
日志:
2016-03-03 00:59:54.867 WebservicePOC[23364:1679678] Downloading file : master
2016-03-03 00:59:56.127 WebservicePOC[23364:1679673] Downloading file : VacationSpots_Complete.zip
2016-03-03 00:59:56.822 WebservicePOC[23364:1679673] File :VacationSpots_Complete.zip Progress: 0.228680 percent
2016-03-03 00:59:57.257 WebservicePOC[23364:1679736] Downloading file : Weather_Final.zip
2016-03-03 00:59:57.357 WebservicePOC[23364:1679678] File :VacationSpots_Complete.zip Progress: 0.457359 percent
2016-03-03 00:59:57.490 WebservicePOC[23364:1679676] File :Weather_Final.zip Progress: 0.139399 percent
2016-03-03 00:59:57.954 WebservicePOC[23364:1679673] File :VacationSpots_Complete.zip Progress: 0.686039 percent
2016-03-03 00:59:58.023 WebservicePOC[23364:1679676] File :Weather_Final.zip Progress: 0.350538 percent
2016-03-03 00:59:58.664 WebservicePOC[23364:1679676] File :master Progress: 34.184551 percent
2016-03-03 00:59:59.483 WebservicePOC[23364:1679779] File :master Progress: 55.647816 percent
2016-03-03 00:59:59.483 WebservicePOC[23364:1679736] File :Weather_Final.zip Progress: 0.561678 percent
2016-03-03 00:59:59.579 WebservicePOC[23364:1679676] File :master Progress: 100.000000 percent
2016-03-03 00:59:59.699 WebservicePOC[23364:1679676] File :Weather_Final.zip Progress: 0.772818 percent
2016-03-03 01:00:00.200 WebservicePOC[23364:1679676] File :Weather_Final.zip Progress: 0.983957 percent
2016-03-03 01:00:00.560 WebservicePOC[23364:1679736] File :Weather_Final.zip Progress: 1.195097 percent
2016-03-03 01:00:01.023 WebservicePOC[23364:1679779] File :Weather_Final.zip Progress: 1.406237 percent
2016-03-03 01:00:01.483 WebservicePOC[23364:1679678] File :Weather_Final.zip Progress: 2.250795 percent
2016-03-03 01:00:01.819 WebservicePOC[23364:1679779] File :Weather_Final.zip Progress: 2.673075 percent
2016-03-03 01:00:02.330 WebservicePOC[23364:1679678] File :Weather_Final.zip Progress: 3.306494 percent
2016-03-03 01:00:02.793 WebservicePOC[23364:1679779] File :Weather_Final.zip Progress: 3.517633 percent
2016-03-03 01:00:03.303 WebservicePOC[23364:1679779] File :Weather_Final.zip Progress: 4.573332 percent
2016-03-03 01:00:03.428 WebservicePOC[23364:1679736] File :Weather_Final.zip Progress: 4.784471 percent
2016-03-03 01:00:04.194 WebservicePOC[23364:1679678] File :Weather_Final.zip Progress: 5.417891 percent
2016-03-03 01:00:04.706 WebservicePOC[23364:1679736] File :VacationSpots_Complete.zip Progress: 0.914719 percent
2016-03-03 01:00:05.604 WebservicePOC[23364:1679736] File :Weather_Final.zip Progress: 5.629030 percent
2016-03-03 01:00:06.235 WebservicePOC[23364:1679678] File :Weather_Final.zip Progress: 5.840169 percent
答案 0 :(得分:0)
您遇到的问题是您在运行startDownloadingZipFileAtUrl
方法的操作队列上设置了最大并发计数。该方法在另一个线程中启动下载,然后返回,允许您的operationqueue运行下一个任务(开始另一个下载)。
您需要做的是在下载队列上设置最大并发任务限制。 AFNetworking没有内置的方法(reference)。您的任务有completionHandler
,您可以在其中开始下一次下载。既然已经内置了它,那么它可能是最简单的方法。否则,请检查几个句子的引用,看看这些选项是否适合你。