How to upload multiple images in one session and different download tasks

I am trying to upload multiple images using the same session and different download tasks as the question. I can upload the first image, but not the second. In didFinishDownloadingToURL I use if the condition for identifying downloadTask and for a specific downloadTask set it to a specific 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 file:

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

+6
source share
2 answers

The code is good, because the problem is that I made a mistake in my actual code, in the second resume I called the first task.

+1
source

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

  • It is not necessary to have so many properties for an instance just below.

@property NSURLSession * session;

You can also define a task through taskIdentifier

NSNumber * key = @ (firstDownloadTask.taskIdentifier);

  1. You need to be careful when performing the ui operation on the delegate, this can cause a hang if not called from the main thread. To be safe, you should use:

dispatch_async (dispatch_get_main_queue (), ^ {

 [firstImageHolder setImage:theImage1]; 

});

Working code example

 @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 
+4
source

Source: https://habr.com/ru/post/981462/


All Articles