1

I have some issues with UIProgressView, I dont know why It crashes. In my view controller I have outlet like:

   @IBOutlet weak var progressBar: UIProgressView!

and I double checked it connected correctly. In my viewDidLoad I reset the value like:

 progressBar.progress = 0.0

Then when I start to download something, I call it like:

func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {

    let written = byteFormatter.string(fromByteCount: totalBytesWritten)
    print("print the written \(written)")
    let expected = byteFormatter.string(fromByteCount: totalBytesExpectedToWrite)
    self.progressBar.progress = Float(100 * bytesWritten/totalBytesExpectedToWrite)

}

but it crash always in this line:

 self.progressBar.progress = Float(100 * bytesWritten/totalBytesExpectedToWrite)

Any idea?

Steven
  • 762
  • 1
  • 10
  • 27

1 Answers1

1

Try this way

DispatchQueue.main.async {
    if self.progressBar != nil{
       self.progressBar.progress = Float(100 * bytesWritten/totalBytesExpectedToWrite)
     }
}
  • Nope, the error message Cannot assign to value: 'progreBar' is a 'let' constant – Steven Jul 04 '18 at 16:29
  • Try to use above lines now, let me know if it helps – Hussain Chhatriwala Jul 04 '18 at 16:31
  • 1
    That will not work, because it will shows always nil. – Steven Jul 05 '18 at 07:40
  • @Steven I asked to do so because there is crash due to optional handling, so if it dose not go in inner lines then surely there is problem in configuration of progress view. It if it not that case then code would run without any obstacles. – Hussain Chhatriwala Jul 05 '18 at 07:43
  • I tried but it doesnt work, probably when I run the download with viewWillAppear, the progress not work because the view not set. it can be some bug of apple. when I try with click button when view set, it works. I can run when viewdidappear but in this case will not help me, because make late the download. – Steven Jul 05 '18 at 09:49
  • @Steven try it on main thread once as mentioned above, let me know if that works. – Hussain Chhatriwala Jul 05 '18 at 10:08
  • Checking for `nil` is not *swifty*, there is optional chaining: self.progressBar?.progress = ...` and remove the `if` statement. – vadian Jul 05 '18 at 10:18
  • I tried @Hussain Chhatriwala, it doesnt crash but the progress doesnt work. – Steven Jul 05 '18 at 12:17