Reputation: 91
Essentially I want a label to appear as a subview when I add it and not wait until the current thread has finished what it is doing. In the below example, the label appears in the UI of the app at the same time 'Awake' is printed. Do you know how I can get the subview to appear in the UI before the thread sleeps?
@IBAction func ButtonTapped(_ sender: Any) {
let label = UILabel(frame: self.view.bounds)
label.text = "Label Text"
self.view.addSubview(label) // I want this to appear on the UI before...
sleep(3)
print("Awake") // ... this is printed
}
And enclosing the addSubView() line within DispatchQueue.main.async {} doesn't fix the issue.
Thanks.
Upvotes: 1
Views: 146
Reputation: 91
Thanks to Paulw11, I figured out that I need to do the processing on a background thread. Simple solution in swift:
@IBAction func ButtonTapped(_ sender: Any) {
let label = UILabel(frame: self.view.bounds)
label.text = "Label Text"
self.view.addSubview(label) // I want this to appear on the UI before...
DispatchQueue.global(qos: .background).async {
sleep(3)
print("Awake") // ... this is printed
}
}
Upvotes: 1