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

  • 约会电话(日期)
  • 约会详情()
  • firmDetails()
  • unitData()
  • unitDataHistory()
  • unitDataImages()
  • 下载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)
                }
            }
          }
      }
    

    最佳答案

    添加属性观察器downloadProgress

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

    然后为其分配proges的值。每当进度值更改时,就会调用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)
                })
    

    10-06 04:21
    查看更多