Observe progress of Data download in Swift?

前端 未结 1 1726
暗喜
暗喜 2021-01-19 21:50

I need to cache data that I receive from an remote url to a local URL. I am able to do this successfully with:

let dataToCache = try Data(contentsOf: url) try

相关标签:
1条回答
  • 2021-01-19 22:26

    Don't use Data(contentsOf: url) to make synchronous request for urls that are not local resources. You should use URLSession.To observe the progress you need to set urlsessiondownloaddelegate https://developer.apple.com/documentation/foundation/urlsessiondownloaddelegate and to monitor the progress, use the method

    func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64)
    

    https://developer.apple.com/documentation/foundation/urlsessiondownloaddelegate/1409408-urlsession

    0 讨论(0)
提交回复
热议问题