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

标签 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. 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 中得到修复

关于swift - Alamofire:缓存下载到 Documents 文件夹的大文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44077080/

相关文章:

pod 更新到 3.4.1 后 xcode 8 beta Alamofire 编译器错误

ios - 用于 Swift 2.0 的 Alamofire 和 SwiftyJSON

swift - 使用 presentScene() 更改 SKScene

ios - 在 spritekit 中我无法弹出 Game Center View

java - C#服务器和JAVA客户端之间的网络通信: how to handle that.过渡?

c - 如何将程序中定义的默认端口号添加到 getaddrinfo()

ios - 如何快速打印 Alamofire 中发布请求的响应的 json 内容?

ios - 我的节点没有发生冲突

ios - 带有名为 `imageView` 的 IBOutlet 的 UITableViewCell 与父类(super class) IBOutlet 冲突的错误

linux - 从 AS 生成 ipset 表