I want to show download progress in table view cell, but when i am coming back from another controller then progress bar not showing updated value in cell. It'll show only last received value. I tried to update progress value using NOTIFICATION CENTER and DELEGATE, but both are not working. How can I achieve this. Please help me.
Here is my code which i implemented in my project.
startDownloading methods is implented in common class of my project
func startDownloading(url: String, indexpath: IndexPath) {
if (NetworkReachabilityManager()!.isReachable) {
let destination = DownloadRequest.suggestedDownloadDestination(for: .documentDirectory)
Alamofire.download(URL(string: url)!, method: .get, parameters: nil, encoding: JSONEncoding.default, headers: nil, to: destination).downloadProgress(queue: .main) { (progress) in
DispatchQueue.main.async {
print("download: \(progress)")
NotificationCenter.default.post(name: Notification.Name("downloadSong"), object: ["row": indexpath.row, "value": progress])
}
}.response(queue: .global(qos: .background)) { (response) in
if let destinationPath = response.destinationURL {
NotificationCenter.default.post(name: Notification.Name("downloadSong"), object: indexpath.row)
NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: "downloadSong"), object: nil)
}
}
} else {
print("Please connect to Internet.")
}
}
and following method implemented in UIViewcontroller class:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier:"cellReuseIdentifier") as! AllSongsTableCell
cell.downloadTapped = {
NotificationCenter.default.addObserver(self, selector: #selector(self.updateCell(_:)), name: NSNotification.Name("downloadSong"), object: nil)
startDownloading(url: "song-url", indexpath: indexPath)
}
}
//Observer Method
@objc func updateCell(_ notification: NSNotification) {
if let object = notification.object as? [String: Any] {
DispatchQueue.main.async {
if let cell = self.allSongsTable.cellForRow(at: IndexPath(row: object["row"] as! Int, section: 0)) as? AllSongsTableCell {
let value = object["value"] as! Double
cell.graphView.value = UICircularProgressRing.ProgressValue(value)
}
self.allSongsTable.reloadRows(at: [IndexPath(row: object, section: 0)], with: .none)
}
}
}