2

I have implemented tableView with custom cell for appointments with progress bar for tracking. How can I observe or track progress bar on swift functions? Actually I am not getting exact idea how I save progress data and how to show it? I have functions hierarchy like this.

  1. appointmentCall(date)
    • appointmentDetail()
    • firmDetails()
    • unitData()
    • unitDataHistory()
    • unitDataImages()
    • downloadPDFTask(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.

How I track in progress bar ?

downloadPDFTask code:

@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)
                })
    }

Update Code & Image:

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)
            }
        }
      }
  }
Newbie
  • 360
  • 3
  • 19

2 Answers2

2

Add a property observer downloadProgress

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

and then assign it the value of progess. didSet will be called every time the progress value changes.

    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)
            })
Keshu R.
  • 5,045
  • 1
  • 18
  • 38
1

You are already calling the Progress API, so the only thing you have to do is expose it to your method’s caller.


@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)
                })
    }

That way, you add a closure that receives a Double between 0 and 1 indicating the progress. In your call, you pass a closire that updates your progress bar.