2

i need to download multiple files in queue. When i try to download single file it works perfectly fine, however when i try to download multiple file it start downloading at the same time with first download, i need to keep them in queue until first download is finished and then work with second download. I am using AFNetworking Library extension AFDownloadRequestOperation. Below is my code. Please help me regarding this, if there is any thing i am doing wrong.

         NSURLRequest *request = [NSURLRequest requestWithURL:videoURL];

         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];
             }
         }];

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

             float percentDone = ((float)totalBytesRead) / totalBytesExpectedToReadForFile;
             delegate.progressDone = percentDone;
             [progressTableView reloadData];
         }];
         [operation start];
user748001
  • 181
  • 6
  • 21
  • Best is to check the AFDownloadRequestOperation documentation on how to do it. If that does not help, then start the second request from the success or failure completion block of the first request. Is there a specific reason why you want to do them in sequence? – fishinear Mar 11 '13 at 13:33
  • have a look at http://stackoverflow.com/questions/11186854/download-a-file-image-with-afnetworking-in-ios – Max MacLeod Mar 11 '13 at 14:13
  • I'm not familiar with the AF* classes, but if it is a true subclass of [NSOperation] you could integrate with [NSOperationQueue] fairly simply. Or perhaps the AF* classes already do. – EricLeaf Mar 11 '13 at 14:55

1 Answers1

2

Create the requests and add them to an instance of AFHTTPClient's operation queue:

AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:nil];

// Important if only downloading one file at a time
[client.operationQueue setMaxConcurrentOperationCount: 1];

NSArray *videoURLs; // An array of strings you want to download

for (NSString * videoURL in videoURLs) {

    // …setup your requests as before

    [client enqueueHTTPRequestOperation:downloadRequest];
}

Your first request will start as soon as it is added to AFHTTPClient's operation queue but subsequent requests will wait in turn for the previous operation to end.

  • @Matt Thanks for Structure .. i got failed error after sometimes because i was creating AfhttpClient object inside of for loop and because of that every operation having its own client object and thats why i got fail error..thanks too you i solve my problem – Swap-IOS-Android Aug 21 '13 at 10:32