Alamofire:缓存下载到Documents文件夹的大文件

时间:2017-05-19 18:49:26

标签: swift networking alamofire

我使用这段代码将MP3文件下载到文档目录:

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

该文件下载正常,但如果我关闭应用程序并再次启动它,则下载从0重新开始。我想要的是立即缓存和获取文件。 我对Alamofire的理解是文件被下载到一个临时文件夹,然后移动到Documents文件夹,这是什么导致缓存不发生?

非常感谢

1 个答案:

答案 0 :(得分:7)

请按顺序检查以下参考:
1. https://github.com/Alamofire/Alamofire/issues/1104
2. https://github.com/Alamofire/Alamofire#resuming-a-download
3. https://stackoverflow.com/a/39347461/3549695

总结:
您需要使用request.cancel()API在应用退出之前生成resumeData。
然后使用resume API和resumeData在应用程序重新启动时恢复请求。
无法保证它始终有效。如果失败,那么它仍将从0重新启动。

上面的参考文献(2)中提到了iOS 10的问题,导致其无法正常工作。 但StackOverflow上的更新(参考文献3)有一个报告,它已在iOS 10.2中修复

相关问题