3

我需要在队列中下载多个文件。现在我的代码正在运行并且所有文件正在同时下载,但是我需要一次下载一个文件,并且所有其他文件都在队列中,下面是代码,请让我知道我做错了什么。我只需要一次下载一个文件,所有其他文件都应该在队列中。

         AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:videoURL];
         [httpClient.operationQueue setMaxConcurrentOperationCount:1];
         for (NSURL *videoString in videoArray) {

             NSURLRequest *request = [NSURLRequest requestWithURL:videoString];

             AFDownloadRequestOperation *operation = [[AFDownloadRequestOperation alloc] initWithRequest:request targetPath:path shouldResume:YES];
             [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
                 if(operation.response.statusCode == 200) {

                     UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"Successfully Downloaded" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
                     [alertView show];

                  }

             }failure:^(AFHTTPRequestOperation *operation, NSError *error) {

                 if(operation.response.statusCode!= 200) {

                     UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"Error While Downloaded" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
                     [alertView show];
                 }
             }];

             [httpClient enqueueHTTPRequestOperation:operation];

             [operation setProgressiveDownloadProgressBlock:^(NSInteger bytesRead, long long totalBytesRead, long long totalBytesExpected, long long totalBytesReadForFile, long long totalBytesExpectedToReadForFile) {

                 float percentDone = ((float)totalBytesRead) / totalBytesExpectedToReadForFile;

                 }
             }];
4

2 回答 2

1

要将队列一次限制为一个操作,

您可以在排队之前尝试在每个操作之间添加依赖项。

像这样

[Operation2 addDependency:Operation1];

希望有帮助!

于 2013-03-15T10:42:19.773 回答
0

在块之后使用调度队列(GCD)setCompletionBlockWithSuccess:

像以下方式:

 dispatch_async(dispatch_get_main_queue(), ^{
// Call any method from on the instance that created the operation here.
  [self doSomework]; // example
 });
于 2013-03-15T10:53:12.350 回答