Hi,
I have a problem. I'd like to update files from server, and the downloadTaskWithRequest:progress:destination:completionHandler: method doesn't overwrite the existing files.
Do you have any idea how to do this?
My idea for a workaround is to delete the existing file in the destination block, but there must be a better solution.
Thanks!
My idea for a workaround is to delete the existing file in the destination block, but there must be a better solution.
That would be the suggested resolution. Having downloadTaskWithRequest:progress:destination:completionHandler: overwrite existing files feels like overstepping expectations. So yeah, that block would be the right place to put that kind of logic.
It should be mentioned that an existing file must not be overwritten in this way if an HTTP error occurs.
So check HTTP status code in destination block.
destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
if ([httpResponse statusCode] == 200) {
// delete existing file
}
}
destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
documentsDirectoryURL = [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
if ([httpResponse statusCode] == 200) {
// delete existing file
NSError *error;
if ([fileManager isDeletableFileAtPath:documentsDirectoryURL.path]) {
BOOL isFileDeleted = [fileManager removeItemAtPath:documentsDirectoryURL.path error:&error];
if (isFileDeleted) {
NSLog(@"Deleted Existing File");
}
}
}
Most helpful comment
It should be mentioned that an existing file must not be overwritten in this way if an HTTP error occurs.
So check HTTP status code in
destinationblock.