Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alamofire: cache large files downloaded to the Documents folder

I use this piece of code to download MP3 files to the documents directory:

let destination = DownloadRequest.suggestedDownloadDestination(for: .documentDirectory)
Alamofire.download(theUrl!, to:destination).response { response in
    // stuff
}.downloadProgress { progress in
    // Stuff
}

The file downloads fine, but if i close the app and start it again, the download restarts from 0. What i want is for the file to be cached and fetched immediatly. My understanding of Alamofire is that the file is downloaded to a temporary folder and then moved to the Documents folder, is this what causes the cache to not happend ?

Thanks a lot

like image 921
Rogue Avatar asked Jan 05 '23 00:01

Rogue


1 Answers

Please check the following references in order:
1. https://github.com/Alamofire/Alamofire/issues/1104
2. https://github.com/Alamofire/Alamofire#resuming-a-download
3. https://stackoverflow.com/a/39347461/3549695

In summary:
You need to use the request.cancel() API to generate the resumeData before your app exit.
Then you use the resume API with the resumeData to resume the request when the app restart.
There is no guarantee that it will always work. If it fails then it will still restart from 0.

There was a mentioning of issue with iOS 10 in reference (2) above that prevent this from working properly. But the update on StackOverflow (ref. 3) has a report that it has been fixed in iOS 10.2

like image 170
Wismin Avatar answered Jan 13 '23 11:01

Wismin