I upload a zip file to a folder in my application. Watching download progress from 1% to 100% in my label . After the label = 100% I do not find the file in the folder. It appears after 50 seconds. How can I constantly check the presence of a file for 50 seconds in order to know the exact code it will appear in the folder?

boot code

 -(IBAction) downloadButton:(id)sender { if (_HighScore == 2) { _url1 =[NSURL URLWithString:@"link2.zip"]; _downloadTask1 = [_session downloadTaskWithURL:_url1]; [_downloadTask1 resume]; } - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location { if (downloadTask == _downloadTask1) { _documentsDirectory1 = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; _zipPath1 = [_documentsDirectory1 stringByAppendingPathComponent:@"2.zip"]; } dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ if (downloadTask == _downloadTask1) { NSData *urlData1 = [NSData dataWithContentsOfURL:_url1]; [urlData1 writeToFile:_zipPath1 atomically:YES];} }); } 

UPD

 _documentsDirectory1 = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; _zipPath1 = [_documentsDirectory1 stringByAppendingPathComponent:@"2.zip"]; _destinationPath1 = [_documentsDirectory1 stringByAppendingPathComponent:@"MediaData"]; _fileExists1 = [[NSFileManager defaultManager] fileExistsAtPath:_zipPath1 isDirectory:false]; if( [SSZipArchive unzipFileAtPath:_zipPath1 toDestination:_destinationPath1] != NO ) { NSLog(@"Dilip Success"); }else{ NSLog(@"Dilip Error"); } 
  • How does it appear after 50 seconds? But what does he do after the end of the download and before the appearance? - Max Mikheyenko
  • @MaxMikheyenko I do not know. Just after 100% I spotted the time after which I see it in the folder. It was assumed that after the label = 100% the end of the load is called. Time after the download can be reduced? - User
  • after the download is complete, the file should be available instantly. you have something wrong there. you can put together an example of the project and post it - Max Mikheyenko
  • stand. so you two times save. first with downloadTaskWithUrl, and when it is executed, you run dataWithContentsOfURL and download it again. - Max Mikheyenko
  • you in didFinishDownloadingToUrl comes location. there is a file there and must lie - Max Mikheyenko

1 answer 1

as I already wrote in the comment, you have two downloads - you download the first one through the NSSession, and when it has finished downloading and done

 - (void)URLSession: downloadTask: didFinishDownloadingToURL: 

you download again using

 [NSData dataWithContentsOfURL:_url1] 

What you need to do in didFinishDownloadingToURL just look where the file is downloaded and from there to take. like this:

 - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); NSURL *documentsFolder = [paths objectAtIndex:0]; NSFileManager *fileManager = [NSFileManager defaultManager]; NSURL *newLocation = [NSURL URLWithString:[NSString stringWithFormat:@"file://%@/myCoolFile.txt", documentsFolder]]; NSError *error; [fileManager copyItemAtURL:location toURL:newLocation error:&error]; } 
  • If I go to the folder along the path NSLog(@«вот файл %@«, location.absoluteString); there will only be a tmp file. my zip is not there. So it should be? - User
  • this is what the documentation tells us: A file URL for the temporary file. If you’re really a little girl, you’ll have to do it. If you choose, you must always be able to read the delegate queue. - Max Mikheyenko
  • in other words, you need to copy it into your application folder - Max Mikheyenko
  • How to do it in code? can you show please? did not find anywhere how to do it - User
  • added in response - Max Mikheyenko