I bet this is a pretty basic question, but I can't get this to work. I am downloading an image from AWS S3, am able to get the progress of the download, and able to have a progress variable update accordingly. However, I cannot find a way to animate a progress bar based on this variable.
The (admittedly clunky) code is as follows:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.refresh() //re-gets user info. Having trouble with AWS randomly claiming I'm attempting to download using UnAuth even though I am logged in
tableView.deselectRow(at: indexPath, animated: true)
self.downloadProgressView.isHidden = false
self.proprietaryObject = proprietaryObjects[indexPath.row]
let expression = AWSS3TransferUtilityDownloadExpression()
expression.progressBlock = {(task, progress) in DispatchQueue.main.async(execute: {
self.progress = progress
})
}
let transferUtility = AWSS3TransferUtility.default()
let resource = "\(self.proprietaryObject?.value1 ?? "NA")_\(self.proprietaryObject?.value2 ?? "NA")_\(self.proprietaryObject?.firstName ?? "NA")_\(self.proprietaryObject?.lastName ?? "NA")"
let type = "jpeg"
self.downloadingFileURL = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("\(resource).\(type)")
transferUtility.download(to: self.downloadingFileURL!, bucket: AWSS3BucketName, key: "\(resource).\(type)", expression: expression, completionHandler: { (task, URL, Data, Error) in
if Error != nil {
print (Error!)
}
if let data = Data {
print("Data: \(data)")
let image = UIImage(data: data)
self.downloadedImage = image
}
}).continueWith(executor: AWSExecutor.default()) { (task) -> Any? in
if let error = task.error {
print("download error: \(error)")
self.refresh()
}else {
print("Download started")
print("User: \(self.user?.username ?? "NO USER")")
self.animatePath()
}
return nil
}
}
func animatePath() {
print("Animation Started")
if !(self.view.subviews.contains(downloadProgressView)) {
self.view.addSubview(downloadProgressView)
downloadProgressView.center = self.view.center
self.downloadProgressView.isHidden = false
self.downloadProgressView.layer.addSublayer(self.shapeLayer)
}else {
self.view.bringSubview(toFront: self.downloadProgressView)
self.downloadProgressView.isHidden = false
downloadProgressView.center = self.view.center
if !((downloadProgressView.layer.sublayers?.contains(self.shapeLayer))!) {
self.downloadProgressView.layer.addSublayer(self.shapeLayer)
}
}
func animate() {
if self.progress?.fractionCompleted == nil {
self.fraction = 0
}else {
self.fraction = 0 + (self.progress?.fractionCompleted)!
}
let animation = CABasicAnimation(keyPath: "strokeEnd")
animation.fromValue = CGFloat(self.shapeLayer.strokeEnd)
animation.toValue = CGFloat(self.fraction!)
animation.duration = 0.2
animation.fillMode = kCAFillModeForwards
self.shapeLayer.strokeEnd = CGFloat(self.fraction!)
self.shapeLayer.add(animation, forKey: "animation")
}
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1) , execute: {
//the one second delay gives self.progress.fractionCompleted time to be non-nil
repeat {
animate()
print("Completed: \((self.progress?.fractionCompleted)! * 100)%")
} while self.fraction! < 1
if self.fraction == 1 {
print("fraction achieved 100%. resetting to 0%")
self.fraction = 0
self.progress = nil
self.performSegue(withIdentifier: "proprietarySegue", sender: self)
}
})
}
The console logs the incrementing ".fractionCompleted" % just fine and when it reaches 100% it does perform the segue. The image in the following ViewController's UIImageView is indeed the downloaded image (override prepare for segue not shown). But I am getting only flashes of the progress circle. Usually, it shows no progress then performs segue. Immediately after the first segue, for each subsequent download either the downloadProgressView doesn't appear or shows 100% completion before it disappears.
I have a feeling I'm doing something fundamentally wrong and / or it has something to do with the thread...?
Any help would be appreciated.