Press ESC to close

Swift Main Thread Error

Hello friends. In this article, I will tell you about an error that occurs when processing with Swift. This error appears as follows.

UILabel.text must be used from main thread only

After pulling more data from the internet, if we do something on the UI, we encounter this error. The reason for this is this. iOS does not lock the screen when extracting data from the internet, so you can still perform operations. Because this time may vary depending on your internet speed or the response time of the web service. For example, if it will take 10 seconds and you lock the screen during this process, it will cause trouble in terms of use.

Since a separate thread is opened for data extraction from the web service and you then update the UI via this thread, you actually encounter this error. For this, you need to run it with DispatchQueue.main.async. Apple explains this very well in their documentation. If you want more information, you can find it here.

If you have questions, you can reach us by sending an e-mail or comment. Good work.


Comments (1)

Leave a Reply

Your email address will not be published. Required fields are marked *