2

我已经实现了带有自定义单元格的tableView,用于带有进度条进行跟踪的约会。如何观察或跟踪 swift 函数的进度条?实际上,我不知道如何保存进度数据以及如何显示它?我有这样的功能层次结构。

  1. 约会电话(日期)
    • 约会细节()
    • 公司详情()
    • 单位数据()
    • 单位数据历史()
    • 单元数据图像()
    • 下载PDFTask(pdfURL: String)

所有功能都非常有效地完成,但downloadPDFTask功能需要很少的时间来处理文件。下载 PDF 正在使用alamofire并且有进度只想跟踪它。

我如何跟踪进度条?

下载PDF任务代码:

@objc public func downloadFile(url:String, filetype: String, callback:@escaping (_ success:Bool, _ result:Any?)->(Bool)) -> Void {
var destination = DownloadRequest.suggestedDownloadDestination(for: .documentDirectory)
if filetype.elementsEqual(".pdf"){
            destination = { _, _ in
                let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
                let downloadFileName = url.filName()
                let fileURL = documentsURL.appendingPathComponent("\(downloadFileName).pdf")
                return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
            }
        } 

        self.request = Alamofire.download(
            url,
            method: .get,
            parameters: nil,
            encoding: JSONEncoding.default,
            headers: nil,
            to: destination).downloadProgress(closure: { (progress) in

                print(progress)
                print(progress.fractionCompleted)

            }).response(completionHandler: { (DefaultDownloadResponse) in
                callback(DefaultDownloadResponse.response?.statusCode == 200, DefaultDownloadResponse.destinationURL?.path)
           print(DefaultDownloadResponse)
                })
    }

更新代码和图像:

var downloadProgress : Double = 0.0 {
      didSet {
        for indexPath in self.tableView.indexPathsForVisibleRows ?? [] {
        if let cell = self.tableView.cellForRow(at: indexPath) as? DownloadEntryViewCell {
            cell.individualProgress.setProgress(Float(downloadProgress), animated: true) //= "\(downloadProgress)" // do whatever you want here
            print(downloadProgress)
            }
        }
      }
  }
4

2 回答 2

2

添加属性观察者downloadProgress

var downloadProgress : Double = 0.0 { 
    didSet { 
        self.tableView.reloadRows(at: [IndexPath(item: yourRow, section: 0)], with: .none) // do whatever you want here
    }
}

然后给它赋值progess。didSet每次进度值更改时都会调用。

    self.request = Alamofire.download(
        url,
        method: .get,
        parameters: nil,
        encoding: JSONEncoding.default,
        headers: nil,
        to: destination).downloadProgress(closure: { (progress) in

            print(progress)
            print(progress.fractionCompleted)
            self.downloadProgress = progress // assign the value here. didSet will be called everytime the progress value changes.
        }).response(completionHandler: { (DefaultDownloadResponse) in
            callback(DefaultDownloadResponse.response?.statusCode == 200, DefaultDownloadResponse.destinationURL?.path)
       print(DefaultDownloadResponse)
            })
于 2020-02-04T12:07:17.637 回答
1

您已经在调用 Progress API,因此您唯一需要做的就是将其公开给您的方法的调用者。


@objc public func downloadFile(url:String, filetype: String, updateProgress: @escaping (_ fraction: Double)->(Void), callback:@escaping (_ success:Bool, _ result:Any?)->(Bool)) -> Void {
var destination = DownloadRequest.suggestedDownloadDestination(for: .documentDirectory)
if filetype.elementsEqual(".pdf"){
            destination = { _, _ in
                let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
                let downloadFileName = url.filName()
                let fileURL = documentsURL.appendingPathComponent("\(downloadFileName).pdf")
                return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
            }
        } 

        self.request = Alamofire.download(
            url,
            method: .get,
            parameters: nil,
            encoding: JSONEncoding.default,
            headers: nil,
            to: destination).downloadProgress(closure: { (progress) in

                print(progress)
                print(progress.fractionCompleted)
                // Call the update closure
                updateProgress(progress.fractionCompleted)

            }).response(completionHandler: { (DefaultDownloadResponse) in
                callback(DefaultDownloadResponse.response?.statusCode == 200, DefaultDownloadResponse.destinationURL?.path)
           print(DefaultDownloadResponse)
                })
    }

这样,您添加了一个闭包,该闭包接收一个介于 0 和 1 之间的 Double 来指示进度。在您的通话中,您传递了一个更新进度条的闭包。

于 2020-02-04T12:20:22.953 回答