Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clean nsurlsession downloadTask generate tmp File?

If I create a DownloadTask by nsurlsession, there was a tmp file named like 'CFNetworkDownload_1vY41L.tmp' in /Developer/tmp/ folder.

Then how to delete the tmp file when I delete downloadTask?

Moreover, I don't want to delete all tmp file because there are other downloadTask cache file.

like image 881
Dejauu Avatar asked Sep 12 '25 06:09

Dejauu


1 Answers

Apple's documentation says that the file will be deleted once the download block finishes, check location explanation. And yes it is deleted, at least in iOS 12, you have to move it before it completes, no need to free space.

Example:

let task = self.session.downloadTask(with: request) { [weak self] url, response, error in
   if let error = error {
      ...
    }

    guard let httpResponse = response as? HTTPURLResponse else {
                fatalError("Couldn't get HTTP response")
    }

    if 200..<300 ~= httpResponse.statusCode, let downloadedPath = url {
       // Move file in downloadedPath to a documents or other location
    }
}

downloadPath will have the location of the file.

like image 182
LightMan Avatar answered Sep 14 '25 20:09

LightMan