3

複数のファイルをキューにダウンロードする必要があります。現在、私のコードは機能しており、すべてのファイルが同時にダウンロードされていますが、一度に 1 つのファイルをダウンロードする必要があり、他のすべてのファイルはキューに入っています。以下はコードです。何が間違っているのか教えてください。一度に 1 つのファイルをダウンロードするだけで、他のすべてのファイルはキューに入れられます。

         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

キューを一度に 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 に答える