![](/img/trans.png)
[英]Storing data from an asynchronous closure during URLSession.shared.dataTask
[英]Get Current Progress from URLSession.shared.dataTask
我需要獲取 GET 請求的數據任務的當前進度(已接收和總字節數),以便從此數據制作進度加載指示器。
添加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)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.