NSURLSession Download Task- Progress Bar Issue
<p>I am working with a Non-ARC project, where i have to downloads video's, and once downloaded i will be displaying them. I am using NSURLSession for downloading it. </p>

<p><strong>Problem is in Progress bar</strong>. I can download first two or three file correctly through progress bar when i start downloading fourth file, the progress bar is not updating properly but file s getting downloaded with last deleted file. </p>

<p>Getting crash on this when deleting and downloading again n again</p>

<p><strong>Attempted to create a task in a session that has been invalidated</strong></p>

<pre><code> - (void)startOrPauseDownloadingSingleFile : (UIButton *)sender
  {
    self.part_id_Value = self.part_id;
    m_Web getIconPathForAnimation:self.part_id];

     [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
      NSURL *urlVideo=[NSURL URLWithString:self.NonInteractiveMP4URL];
      NSString *strlastcomponent=[urlVideo lastPathComponent];


      FileDownloadInfo *fdi = [[FileDownloadInfo alloc] initWithFileTitle:strlastcomponent andDownloadSource:[NSString stringWithFormat:@"%@",urlVideo]];



        if (!fdi.isDownloading)
        {
            // Check if should create a new download task using a URL, or using resume data.


            if (fdi.taskIdentifier == -1)
            {
             **Getting Crash Here**

                fdi.downloadTask = [self.session downloadTaskWithURL:[NSURL URLWithString:fdi.downloadSource]];
            }
            else
            {
                fdi.downloadTask = [self.session downloadTaskWithResumeData:fdi.taskResumeData];
            }

            // Keep the new taskIdentifier.
            fdi.taskIdentifier = fdi.downloadTask.taskIdentifier;

            // Start the download.
            [fdi.downloadTask resume];

            // Indicate for each file that is being downloaded.
            fdi.isDownloading = YES;
        }
    }
</code></pre>

<p>My Methods for downloading and updating progress bar -</p>

<pre><code>- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite
 {
   double Progress = (double)totalBytesWritten / (double)totalBytesExpectedToWrite;
   NSLog(@"Progressview progress : %f",Progress);

if (totalBytesExpectedToWrite == NSURLSessionTransferSizeUnknown)
{
    NSLog(@"Unknown transfer size");
}
else
{
    [self performSelectorOnMainThread:@selector(updateProgress:) withObject:[NSString stringWithFormat:@"%f",Progress] waitUntilDone:NO];
 }
}

- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location
 {
 NSError *error;
 NSFileManager *fileManager = [NSFileManager defaultManager];
 NSString *destinationFilename = downloadTask.originalRequest.URL.lastPathComponent;
 NSURL *destinationURL = [self.docDirectoryURL URLByAppendingPathComponent:destinationFilename];

 if ([fileManager fileExistsAtPath:[destinationURL path]])
 {
    [fileManager removeItemAtURL:destinationURL error:nil];
 }
 BOOL success = [fileManager copyItemAtURL:location
                                    toURL:destinationURL
                                    error:&amp;error];
 if (success)
  {
    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
        NSLog(@"Download completed!");
        NSLog(@"destination file name : %@!",destinationFilename);
        NSLog(@"Part id of file name : %@!",self.part_id);
        [self performSelectorOnMainThread:@selector(updateProgress:) withObject:[NSString stringWithFormat:@"1.0"] waitUntilDone:NO];

        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

        NSString *filenameValue=[NSString stringWithFormat:@"%@",destinationFilename];
        NSString *hideExtension=[filenameValue substringToIndex:filenameValue.length-4];
        NSLog(@"filenameValue == %@", filenameValue);

        [self DBUpdateVideoURLStatus:self.part_id andFileName:filenameValue];
        [self DBInsertDownloads:self.part_id andFileName:filenameValue];

        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:NSLocalizedString(@"Success", @"Success message") message:[NSString localizedStringWithFormat:@"%@ downloaded successfully!",hideExtension] delegate:self cancelButtonTitle:NSLocalizedString(@"Ok", @"Ok action") otherButtonTitles:nil];
        alert.tag = 1;
        [alert show];
        [alert release];
        [self.session finishTasksAndInvalidate];
    }];
 }
 else
  {
    NSLog(@"Unable to copy temp file. Error: %@", [error localizedDescription]);
  }
}

-(void)updateProgress:(id)progress
{
  float currentProgress = [progress floatValue];
  [self.downloadProgress setProgress:currentProgress animated:YES];
  [self.downloadProgress setNeedsDisplay];
  [self.tblview reloadData];
}
</code></pre>