It's my first iOS app and I want to update ProgressView using while loop. I need help, It's not working says "UI element cannot be updated in background thread"
Code Block func update() { DispatchQueue.global().async { var current:Int = 0 let total = 100 while (current < total) { current += 1 self.progress.setProgress(Float(current / total), animated: true) self.text.text = "Hello: " + String(current) print(Float(current / total)) sleep(UInt32(0.1)) } } }
Update must occur in main thread
Code Block func update() { DispatchQueue.global().async { var current:Int = 0 let total = 100 while (current < total) { current += 1 DispatchQueue.main.async { // To update UI self.progress.setProgress(Float(current / total), animated: true) self.text.text = "Hello: " + String(current) print(Float(current / total)) } sleep(UInt32(0.1)) } } }