Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't move file after background download: "No such file"

I have an app using a backgroundSessionConfiguration instance of NSURLSession to handle some NSURLSessionDownloadTask tasks. The tasks are created correctly, and finish downloading, but in URLSession:downloadTask:didFinishDownloadingToURL: when I go to move the downloaded file from location to a permanent spot on disk, I sometimes (read: often) get the error:

NSUnderlyingError=0x178247890 "The operation couldn’t be completed. No such file or directory"

It's as though the downloaded file is being cleared out of the temp directory (../Library/Caches/com.apple.nsnetworkd/..) before I get a chance to move it.

Running the operation for the same remote file will behave both ways with all other factors being equal, so I haven't been able to identify anything that would cause it to sometimes work.

Is there anyway to ensure that this file will stick around long enough to move it into place once it's done?

Edit: I'm also seeing this with some frequency for normal (not background) sessions as well

like image 646
farski Avatar asked Nov 27 '13 19:11

farski


2 Answers

The temporary files are deleted automatically after exiting this method, you can try to hold this returned file as an NSData and saves it directly into the desired location.

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

    // Create a NSFileManager instance
    NSFileManager *fileManager = [[NSFileManager alloc] init];

    // Get the documents directory URL
    NSArray *documentURLs = [fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask];
    NSURL *documentsDirectory = [documentURLs firstObject];

    // Get the file name and create a destination URL
    NSString *sendingFileName = [downloadTask.originalRequest.URL lastPathComponent];
    NSURL *destinationUrl = [documentsDirectory URLByAppendingPathComponent:sendingFileName];

    // Hold this file as an NSData and write it to the new location
    NSData *fileData = [NSData dataWithContentsOfURL:location];
    [fileData writeToURL:destinationUrl atomically:NO];
}
like image 171
Douglas Ferreira Avatar answered Oct 10 '22 22:10

Douglas Ferreira


Try this

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

    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSArray *URLs = [fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask];
    NSURL *documentsDirectory = [URLs objectAtIndex:0];
    NSURL *originalURL = [[downloadTask originalRequest] URL];
    if(originalURL)
       destinationUrl = [documentsDirectory URLByAppendingPathComponent:[originalURL lastPathComponent]];

    NSError *errorCopy;

    [fileManager removeItemAtURL:destinationUrl error:NULL];
    BOOL success = [fileManager copyItemAtURL:downloadURL toURL:destinationUrl error:&errorCopy];

    if (success) {

    } else {

    }
}
like image 3
Jacob Davis Cherussery Avatar answered Oct 10 '22 23:10

Jacob Davis Cherussery