Reputation: 69
I need to download multiple files in queue. Right now my code is working and all files are downloading simultaneously, However i need to download one file at a time, and all other files are in queue, below is code, please let me know what i doing wrong. I just need to download one file at a time and all other file should be in queue.
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;
}
}];
Upvotes: 3
Views: 3318
Reputation: 952
use dispatch queues(GCD) after the block setCompletionBlockWithSuccess:
like following way:
dispatch_async(dispatch_get_main_queue(), ^{
// Call any method from on the instance that created the operation here.
[self doSomework]; // example
});
Upvotes: 0
Reputation: 1528
To limit the queue to one Operation at a time,
you could try adding dependencies between each operation before you queue them.
Like this
[Operation2 addDependency:Operation1];
Hope that helps!
Upvotes: 1