我需要将从远程URL接收的数据缓存到本地url。我可以通过以下方式成功地做到这一点:
let dataToCache = try Data(contentsOf: url) try dataToCache.write(to: cacheURL)
但我想知道是否有某种类型的观察器可以用来在写入数据时显示进度视图?
发布于 2017-10-27 06:13:02
不要使用Data(contentsOf: url)
同步请求非本地资源的urls。您应该使用URLSession
.To观察需要设置urlsessiondownloaddelegate https://developer.apple.com/documentation/foundation/urlsessiondownloaddelegate的进度,要监视进度,请使用方法
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64)
https://developer.apple.com/documentation/foundation/urlsessiondownloaddelegate/1409408-urlsession
https://stackoverflow.com/questions/46962037
复制相似问题