Muhammad Salman
Muhammad Salman

Reputation: 553

Download one file at a time using afnetworking

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?

Upvotes: 0

Views: 525

Answers (2)

Nirav D
Nirav D

Reputation: 72410

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.

Upvotes: 2

TheHungryCub
TheHungryCub

Reputation: 1960

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..!

Upvotes: 1

Related Questions