计算文件下载速度?

时间:2014-08-23 15:00:33

标签: ios swift

我试图找出一种方法来计算文件下载的当前下载速度。我的文件管理器的大部分内容都是本教程的Swift实现:

http://www.appcoda.com/background-transfer-service-ios7/

它工作正常,但我想添加一个带有当前下载速度的小标签。

我想应该在 didWriteData 中完成,检查每秒写入的数据量,或者?

我当前的功能是:

func URLSession(session: NSURLSession!, downloadTask: NSURLSessionDownloadTask!, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {

    if(totalBytesExpectedToWrite == NSURLSessionTransferSizeUnknown) {

        println("Unknow File Size!")

    } else {

        let index:Int = self.getFileDownloadInfoIndexWithTaskIdentifier(downloadTask.taskIdentifier)

        let fdi = self.arrFileDownloadData.objectAtIndex(index) as FileDownloadInfo

        NSOperationQueue.mainQueue().addOperationWithBlock({

            fdi.downloadProgress = Float(totalBytesWritten) / Float(totalBytesExpectedToWrite)

            // Get the progress view of the appropriate cell and update its progress.

            let indexPath = NSIndexPath(forRow: index, inSection: 0)

            let cell = self.tableView.cellForRowAtIndexPath(indexPath) as CustomTableViewCell

            var progressView:UIProgressView = cell.viewWithTag(40) as UIProgressView;

            progressView.progress = fdi.downloadProgress

        })

    }

}

提前致谢。

0 个答案:

没有答案