Empowering you to understand your world

How To Update A Widget In The Main Thread In Swift/UIKit

When developing iOS apps using UIKit, widgets (for example: UILabels) must be updated on the main thread. This means that if you are downloading data from a server and trying to display it in a label using ‘labelName.text = stringFromServer’, you may get an error saying ‘UILabel must be updated on the main thread’.

Example code to update the main thread via Swift:

let queue = DispatchQueue(label: “lbPrice”)

queue.async {

DispatchQueue.main.async {

    lbPrice.text = “stringtodisplay”

    }

}

 

Subscribe to our newsletter
Get notified when new content is published