Uiprogressview Progress Update Very Slow Within Alamofire (Async) Call

UIProgressView progress update very slow within AlamoFire (async) call

There's a MUCH better way to do this in Alamofire. You can use the progress closure to automatically get a callback when a data transfer occurs on your data task. Here's an example from the README monitoring progress for a download request. The same applies for a data request.

let progressView = UIProgressView(progressViewStyle: .Bar)

let params = ["foo": "bar"]
let URLString = "http://httpbin.org/get"

let request = Alamofire.request(.GET, URLString, parameters: params)
request.progress { _, _, _ in
progressView.setProgress(request.progress.fractionCompleted, animated: true)
}
request.responseJSON { request, response, json, error in
println(request)
println(response)
println(json)
println(error)
}

NSProgressIndicator Swift - Alamofire

ANSWER

func downloadFile() {
progressIndicator.isHidden = false
progressIndicator.minValue = 0
progressIndicator.maxValue = 10
progressIndicator.isIndeterminate = false

outputTextField.stringValue = ""

let mainURL = "https://somethinghere.org/macos/\ .(comboCellBox.stringValue)"
let destination = DownloadRequest.suggestedDownloadDestination(for: .downloadsDirectory)
Alamofire.download(mainURL, to: destination)
.downloadProgress { progress in
print("Download Progress: \(progress.fractionCompleted)")
self.progressIndicator.doubleValue = progress.fractionCompleted
}
}
.responseJSON { response in
debugPrint(response)
}
}

A table with cells that all have their own progress views is very slow

The upload progress block can be triggered many times per second, and reloading the whole table view is expensive, and inefficient. What you should do instead is to monitor the upload progress in each table view cell individually. Here is how you can do it.
In your tableviewcell:

  var progressTimer: NSTimer?

var download: Download? {
didSet {
guard let download = download else {
// cancel your timer here
return
}
// start NSTimer here to update UIProgressView every second
}
}

func prepareForReuse() {
super.prepareForReuse()
download = nil
}

In your view controller:

override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell,
forRowAtIndexPath indexPath: NSIndexPath) {
cell.download = downloads[indexPath.row]
}


Related Topics



Leave a reply



Submit