Reputation: 27
I get an error on the line of code here: self.TextMessage.insertText(countOfItems) -[UITextView insertText:] must be used from main thread only
I have been struggling to get this to update the text field with the data.
class GameViewController: UIViewController {
@IBOutlet weak var TextMessage: UITextView!
@IBOutlet weak var getUserInput: UITextField!
var userModel = UserModel()
@IBAction func PerformAction(_ sender: Any) {
print("Begin....:" );
if getUserInput.text == "Ready" {
TextMessage.text = "OK Player"
let semaphore = DispatchSemaphore(value:0)
let queue = DispatchQueue(label: "com.run.concurrent", attributes: .concurrent)
queue.asyncAfter(deadline: DispatchTime.now(), execute: {
[weak self] in
guard let self = self else { return }
print("1")
self.userModel.downloadItems()
semaphore.wait(timeout: DispatchTime.now() + 2)
print("2")
semaphore.resume()
let countOfItems = String(self.userModel.users.count)
print("WE PRINT: " + countOfItems)
self.TextMessage.insertText(countOfItems)
})
print("....END" );
}
}
Upvotes: 0
Views: 234
Reputation: 544
You must do UI updates on main Thread.
DispatchQueue.main.async {
self.TextMessage.insertText(countOfItems)
}
Upvotes: 3