0

I have an array which contains different URLs. I want to download one file with a progress bar, than start the next one and so on.

Here is my code that I have so far;

-(void) func:(NSArray *) arry
{

    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    configuration.timeoutIntervalForRequest = 900;
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

    NSMutableArray * downloadedUrl = [[NSMutableArray alloc] init];
    for (NSString * str in arry) {
        NSURL *URL = [NSURL URLWithString:str];
        NSURLRequest *request = [NSURLRequest requestWithURL:URL];

        NSURLSessionDownloadTask downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL (NSURL targetPath, NSURLResponse response) {
             NSURL *tmpDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory() isDirectory:YES];

             return [tmpDirURL URLByAppendingPathComponent:[response suggestedFilename]];
        } completionHandler:^(NSURLResponse response, NSURL filePath, NSError *error) {

            if(error)
            {
                NSLog(@"File Not Dowloaded %@",error);

            }

        }];
        [downloadTask resume];
    }
}

How would you download one file at a time with a progress bar and then remove the url from array?

markwalker_
  • 12,078
  • 7
  • 62
  • 99
Muhammad Salman
  • 543
  • 4
  • 22

2 Answers2

2

Declare one global NSMutableArray of file and used that in the function like below.

-(void) downloadFile {

     NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
     configuration.timeoutIntervalForRequest = 900;
     AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration]; 

     NSURL *URL = [NSURL URLWithString:[self.fileArr firstObject]]; //Here self.fileArr is your global mutable array
     NSURLRequest *request = [NSURLRequest requestWithURL:URL];

     NSURLSessionDownloadTask downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL (NSURL targetPath, NSURLResponse response) {
     NSURL *tmpDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory() isDirectory:YES];

        return [tmpDirURL URLByAppendingPathComponent:[response suggestedFilename]];
     } completionHandler:^(NSURLResponse response, NSURL filePath, NSError *error) {

        if(error)
        {
            NSLog(@"File Not Dowloaded %@",error);
            [self downloadFile];
        }
        else {
            [self.fileArr removeObjectAtIndex:0];
            if (self.fileArr.count > 0) {
                [self downloadFile];
            }
        }
    }];
    [downloadTask resume];
}

Now call this function but before that initialize self.fileArr and after that call downloadFile method.

Hope this will help you.

Nirav D
  • 71,513
  • 12
  • 161
  • 183
  • Even in error of file failed downloading you should continue process for next download and handle failure case as you should. –  Jun 21 '16 at 10:21
  • How to calculate the progress.? @NiravDoctorwala – Muhammad Salman Jun 21 '16 at 10:32
  • @salmancs43 Either use multipart download or assume progress. –  Jun 21 '16 at 10:34
  • @salmancs43 have you try this answer http://stackoverflow.com/questions/19145093/how-to-get-download-progress-in-afnetworking-2-0 – Nirav D Jun 21 '16 at 10:37
1

Give limit the queue to one Operation at a time,

For that, Try to adding dependencies between each operation before you queue them.

If you add dependency between two operations say operation1 and operation2 before adding to queue then the operation2 will not start until operation1 is finished or cancelled.

Do it like:

NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];

// Make operation2 depend on operation1

[operation2 addDependency:operation1];

[operationQueue addOperations:@[operation1, operation2, operation3] waitUntilFinished:NO];

UPDATE

// Create a http operation

NSURL *url = [NSURL URLWithString:@"http://yoururl"];

NSURLRequest *request = [NSURLRequest requestWithURL:url];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

[httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

    // Print the response body in text

    NSLog(@"Response: %@", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);

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

    NSLog(@"Error: %@", error);

}];

// Add the operation to a queue
// It will start once added

NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
[operationQueue addOperation:operation];

hope it help you..!

Suraj Sukale
  • 1,778
  • 1
  • 12
  • 19