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.
- appointmentCall(date)
- appointmentDetail()
- firmDetails()
- unitData()
- unitDataHistory()
- unitDataImages()
- downloadPDFTask(pdfURL: String)
All functions very efficiently completed but
downloadPDFTask
function takes little time for files. download PDF is usingalamofire
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)
}
}
}
}