ios – Loading and displaying the process

Question:

I use this code to load pictures

-(IBAction) downloadButton:(id)sender {

if(_downloadTask == nil){

            _url1 =[NSURL URLWithString:@"someUrl"];
            _url2 =[NSURL URLWithString:@"someUrl"];
            _downloadTask = [_session downloadTaskWithURL:_url1];
            _downloadTask1 = [_session downloadTaskWithURL:_url2];
           [_downloadTask resume];
           [_downloadTask1 resume];

        }
else
        [_downloadTask resume];
        [_downloadView removeFromSuperview];
        [_downloadButton removeFromSuperview];
        [_downloadButtonCancel removeFromSuperview];

    }
}

- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location
{

    _paths1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    _documentsDirectory1 = [_paths1 objectAtIndex:0];
    _filePath1 = [_documentsDirectory1 stringByAppendingPathComponent:@"1.png"];
    _fileExists1 = [[NSFileManager defaultManager] fileExistsAtPath:_filePath1 isDirectory:false];

    _paths2 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    _documentsDirectory2 = [_paths2 objectAtIndex:0];
    _filePath2 = [_documentsDirectory2 stringByAppendingPathComponent:@"2.png"];
    _fileExists2 = [[NSFileManager defaultManager] fileExistsAtPath:_filePath2 isDirectory:false];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

    NSData *urlData1 = [NSData dataWithContentsOfURL:_url1];
    [urlData1 writeToFile:_filePath1 atomically:YES];

    NSData *urlData2 = [NSData dataWithContentsOfURL:_url2];
    [urlData2 writeToFile:_filePath2 atomically:YES];
});

And this code to display the loading progress as a percentage

    - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten  totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite;
{
    dispatch_async(dispatch_get_main_queue(), ^{

        float progress = [[NSNumber numberWithInteger:totalBytesWritten] floatValue];
        float total = [[NSNumber numberWithInteger:totalBytesExpectedToWrite] floatValue];

        NSString *percentage = [NSString stringWithFormat:@"%.f%%", ((progress / total) * 100)];

        NSLog(@"%.f%%", percentage);

       if (!_label) {



            _label = [[UILabel alloc] initWithFrame:CGRectMake(300, 130, 42, 19)];

            _label.numberOfLines = 1;
            _label.baselineAdjustment = UIBaselineAdjustmentAlignBaselines;
            _label.adjustsFontSizeToFitWidth = YES;
            _label.minimumScaleFactor = 10.0f/12.0f;
            _label.clipsToBounds = YES;
            _label.backgroundColor = [UIColor clearColor];
            _label.textColor = [UIColor whiteColor];
            _label.textAlignment = NSTextAlignmentCenter;

            [self.view addSubview:_label];

        }

        _label.text = percentage;

The problem is that the label doesn't show the loading from 1% to 100%. and randomly shows different numbers. How to fix it?

Answer:

So you load two urls at once, so they both show their progress at the same time. You need to either load them one by one, or use the downloadTask parameter: (NSURLSessionDownloadTask *) downloadTask to determine which task it is and change the corresponding label. You can also maintain one general progress for two URLs.

Scroll to Top