[英]Get Current Progress from URLSession.shared.dataTask
我需要获取 GET 请求的数据任务的当前进度(已接收和总字节数),以便从此数据制作进度加载指示器。
Add URLSessionDownloadDelegate
, create a URLSession
with delegate添加URLSessionDownloadDelegate
,使用委托创建一个URLSession
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 .这是从 Apple 的网站链接复制的。
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. 这是调用您所要求的下载委托方法的链接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.