gpt4 book ai didi

ios - 快速观察或跟踪每个功能上的进度条

转载 作者:行者123 更新时间:2023-12-01 18:35:27 26 4
gpt4 key购买 nike

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

  • 预约电话(日期)
  • 预约详情()
  • 公司详情()
  • unitData()
  • unitDataHistory()
  • unitDataImages()
  • 下载PDFTask(pdfURL: String)

  • All functions very efficiently completed but downloadPDFTaskfunction takes little time for files. download PDF is using alamofire and have the progress only want to track it.



    我如何跟踪进度条?

    下载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
    }
    }

    然后为它分配progress的值。 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)
    })

    关于ios - 快速观察或跟踪每个功能上的进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60056936/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com