【问题标题】:Observe or Track progress bar on every function swift快速观察或跟踪每个功能的进度条
【发布时间】:2020-05-20 06:06:21
【问题描述】:

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

  1. 约会电话(日期)
    • 约会详情()
    • firmDetails()
    • unitData()
    • unitDataHistory()
    • unitDataImages()
    • 下载PDFTask(pdfURL: String)

所有功能都非常有效地完成,但downloadPDFTaskfunction 花费很少的时间来处理文件。下载 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)
            }
        }
      }
  }

【问题讨论】:

    标签: ios swift uitableview alamofire uiprogressbar


    【解决方案1】:

    添加属性观察者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)
                })
    

    【讨论】:

    • 让我执行,我会尽快回复您。
    • didSet 中的一件事提到了textField,但我正在使用带有自定义单元格的tableView。
    • @Newbie 更新了答案。并确保在你的 cellForRowAt 中,你正在为你的值分配这样的东西。 cell.textFielld.text = "\(downloadProgress)"
    • 是的,我已经看到了,我是这样实现的,让我更新我的代码。
    • 您对此有何建议?
    【解决方案2】:

    您已经在调用 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 来指示进度。在您的通话中,您传递了一个更新进度条的闭包。

    【讨论】:

    • 是的,你是对的,但我实际上对在单元格上设置值感到困惑。让我上传表格视图单元格的图像。
    • 根据您发布的图片,我认为其他解决方案将更适合您的需求。我赞成它。
    猜你喜欢
    • 2013-03-17
    • 1970-01-01
    • 1970-01-01
    • 2016-11-28
    • 1970-01-01
    • 2021-09-10
    • 1970-01-01
    • 2015-11-30
    • 1970-01-01
    相关资源
    最近更新 更多