I have a function that can run for a while. While it is running, I want to provide feedback on the screen.
The problem is that the label.text is not updating.
I suspect this is because I need to execute the function in the background, otherwise execution "halts" in the viewDidAppear method. But if I do that, I think updates in the background do not update the text as well. So I think I must update the label.text on the main thread again.
But I get a runtime error when I do this:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var label: UILabel!
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
DispatchQueue.main.async {
for i in 1...10 {
DispatchQueue.main.sync {
self.label.text = "Working on item \(i)..."
}
sleep(1)
}
}
}
}
I had a look at this post, but the methods appear to be dated and do not work in Xcode 10 / Swift 4.