我需要获取 GET 请求的数据任务的当前进度(已接收和总字节数),以便从此数据制作进度加载指示器。
Add URLSessionDownloadDelegate
, create a URLSession
with delegate
URLSession(configuration: configuration, delegate: self, delegateQueue: nil)
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
_progress = Float(totalBytesWritten) / Float(totalBytesExpectedToWrite)
}
There are three types of session tasks. This is copied from Apple's websitelink .
You should use the download task instead of data task because its delegate methods will allow you to track the download progress. Here is the link to the download delegate methods that call what you are asking for.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.