如何显示在后台线程中执行的任务的进度视图
How to show progress view for task getting executed in background thread
我正在使用 NSURLSession's dataTaskWithRequest
将数据下载到我的应用程序。
目前,当我的应用程序从网络服务器下载数据时,我会在我的视图控制器上显示一个 activity 视图叠加层。
我的用户要求我展示一个跟踪下载过程执行完成的自定义动画。
我知道dataTaskWithRequest
是asynchronous
并在background thread
中执行。我也知道我可以 NSURLSessionDelegate
方法
func URLSession(session: NSURLSession, task: NSURLSessionTask, didSendBodyData bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64) {}
跟踪数据上传的进度。所以,我想知道是否有任何方法可以为下载 activity.
分配进度视图
任何建议都会有所帮助。谢谢。
您应该可以使用委托方法
func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten writ: Int64, totalBytesExpectedToWrite exp: Int64)
与didSendBodyData
的工作方式几乎相同
它不会 link 下载到您的进度视图,但设置 progressView.progress = Float(totalBytesWritten) / Float(totalBytesExpectedToWrite)
应该很简单
在该委托方法中。
我正在使用 NSURLSession's dataTaskWithRequest
将数据下载到我的应用程序。
目前,当我的应用程序从网络服务器下载数据时,我会在我的视图控制器上显示一个 activity 视图叠加层。
我的用户要求我展示一个跟踪下载过程执行完成的自定义动画。
我知道dataTaskWithRequest
是asynchronous
并在background thread
中执行。我也知道我可以 NSURLSessionDelegate
方法
func URLSession(session: NSURLSession, task: NSURLSessionTask, didSendBodyData bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64) {}
跟踪数据上传的进度。所以,我想知道是否有任何方法可以为下载 activity.
分配进度视图任何建议都会有所帮助。谢谢。
您应该可以使用委托方法
func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten writ: Int64, totalBytesExpectedToWrite exp: Int64)
与didSendBodyData
它不会 link 下载到您的进度视图,但设置 progressView.progress = Float(totalBytesWritten) / Float(totalBytesExpectedToWrite)
应该很简单
在该委托方法中。