Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to download multiple images using same session and different download tasks

I am trying to download multiple images using the same session and different download tasks as the Question says. I am able to download the first image but not the second one. In the didFinishDownloadingToURL I'am using if condition to identify the downloadTask and for a certain downloadTask set it to a certain imageView.

Here is my code and please be patient with me:

@interface ViewController ()
{
    NSURLSessionConfiguration *sessionConfiguration;
    NSURLSessionDownloadTask *firstDownloadTask;
    NSURLSessionDownloadTask *secondDownloadTask;
    NSURLSession *session;
    UIImageView *firstImageHolder;
    UIImageView *secondImageHolder;
}
@end

- (void)viewDidLoad
{
            NSString *firstDownloadLink = @"http://letiarts.com/letiarts2014/wp-content/uploads/2014/04/icon_game.png";
            sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
            session = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:self delegateQueue:nil];
            firstImageHolder = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 45, 45)];
            [_viewImages addSubview: firstImageHolder];
            firstDownloadTask = [session downloadTaskWithURL:[NSURL URLWithString:firstDownloadLink]];
            [firstDownloadTask resume];

            //2
            NSString *secondDownloadLink = @"http://downloads.bbc.co.uk/skillswise/images/promo/prefab-maths-game-336x189.jpg";
            secondImageHolder = [[UIImageView alloc] initWithFrame:CGRectMake(50, 0, 45, 45)];
            [_viewImages addSubview: secondImageHolder];
            secondDownloadTask = [session downloadTaskWithURL:[NSURL URLWithString:secondDownloadLink]];
            [secondDownloadTask resume];
}

And in the didFinishDownloadingToURL:

- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location {
    NSData *data = [NSData dataWithContentsOfURL:location];

    if (downloadTask == firstDownloadTask) {

            UIImage *theImage1 = [UIImage imageWithData:data];

            [firstImageHolder setImage:theImage1];

        NSLog(@"DOWNLOAD FIRST IMAGE FINISHED");

    }
    //download finished
    if (downloadTask == secondDownloadTask) {

            UIImage *theImage2 = [UIImage imageWithData:data];

            [secondImageHolder setImage:theImage2];

        NSLog(@"DOWNLOAD SECOND IMAGE FINISHED");

    }
}

Thank you in advance!

like image 573
Laur Stefan Avatar asked Jan 23 '15 16:01

Laur Stefan


1 Answers

I see that you already solved your own question, but I would like to add a few things.

  1. There is no need to have so many properties for the instance just below is enough.

@property NSURLSession * session;

Also you can identify the task through taskIdentifier

NSNumber * key = @(firstDownloadTask.taskIdentifier);

  1. You need to be careful while doing ui operation in the delegate, it may cause freeze if not called from the main thread. To be safe, should use:

dispatch_async(dispatch_get_main_queue(), ^{

[firstImageHolder setImage:theImage1];  

});

Sample Working Code

@interface ViewController: ...<...>
@property (nonatomic, strong) NSURLSession *session;
@end

@implementation ViewController

- (void)viewDidLoad
{

        NSURLSessionConfiguration *sessionConfiguration;

        //NSURLSessionDownloadTask *downloadTask; //can use same task if cancelling is not intended.

        NSURLSessionDownloadTask *firstDownloadTask;
        NSURLSessionDownloadTask *secondDownloadTask;

        sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
        session = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:self delegateQueue:nil];

        //1
        NSString *firstDownloadLink = @"http://letiarts.com/letiarts2014/wp-content/uploads/2014/04/icon_game.png";
        firstImageHolder = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 45, 45)];
        [_viewImages addSubview: firstImageHolder];
        /*downloadTask*/firstDownloadTask = [session downloadTaskWithURL:[NSURL URLWithString:firstDownloadLink]];
        [/*downloadTask*/firstDownloadTask resume];

        //2
        NSString *secondDownloadLink = @"http://downloads.bbc.co.uk/skillswise/images/promo/prefab-maths-game-336x189.jpg";
        secondImageHolder = [[UIImageView alloc] initWithFrame:CGRectMake(50, 0, 45, 45)];
        [_viewImages addSubview: secondImageHolder];
        /*downloadTask*/secondDownloadTask = [session downloadTaskWithURL:[NSURL URLWithString:secondDownloadLink]];
        [/*downloadTask*/secondDownloadTask resume];
}

- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location {
    NSData *data = [NSData dataWithContentsOfURL:location];

    if (downloadTask.taskIdentifier == 1) {
        dispatch_async(dispatch_get_main_queue(), ^{
            UIImage *theImage1 = [UIImage imageWithData:data];
            [firstImageHolder setImage:theImage1];
        });

        NSLog(@"DOWNLOAD FIRST IMAGE FINISHED");

    }
    //download finished
    if (downloadTask.taskIdentifier == 2) {
        dispatch_async(dispatch_get_main_queue(), ^{
            UIImage *theImage2 = [UIImage imageWithData:data];
            [secondImageHolder setImage:theImage2];
        });

        NSLog(@"DOWNLOAD SECOND IMAGE FINISHED");

    }
}

@end
like image 180
bllakjakk Avatar answered Sep 21 '22 13:09

bllakjakk