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
Swift Build Error_If_Any_Output_Files_Are_Specified_They_All_Must_Be
(Appkit) Tab Insertion Inside of Nstextblock
How to Save Value in Nsset Core Data (Swift)
Are Static Classes Supported by Swift
Context Menu Not Updating in Swiftui
Testing Protocol Conformance with Associated Types
Swift Client and Root Ssl Certificate Authentication
Bit Field Larger Than 64 Shifts in Swift
Swiftui: How to Implement Edit Menu in MACos App
iOS 13 Modals - Calling Swipe Dismissal Programmatically
Not Getting Expected Delegate Calls When Trying to Restore In-App Purchases with Storekit
Parsing Fetched JSON to Dictionary in Swift 3
How to Simplify Swift Enum Custom Init
Custom Intialiser on Primitive Types for JSONdecoder
Create a New Window with Nswindow