Why update the UI in the main thread (iOS development)

Source: Internet
Author: User
Tags uikit

During the iOS development process, I always knew that updating the UI needed to be in the main thread, but I didn't think much about why it was in the main thread, or why I couldn't update the UI in the child thread. Today I find time to look up the information on the Internet and find out the problem.

Ensuring thread safety on a large framework such as Uikit is a major task that can bring huge costs. Uikit is not thread-safe, if the same background picture is set in two threads, it is likely that the background image will be released two times, causing the program to crash. Or a thread that traverses the search for a subview, but deletes the subview in another thread, causing confusion. Apple has rewritten most of the drawing methods and classes such as Uicolor to be thread-safe, but it is also recommended that UI operations be guaranteed in the main thread.

In fact, if you want to update the other UI in a child thread, you must wait until the child thread has finished running, and the UI update to the button that responds to the user's click is timely, regardless of whether he is updating in the main thread or in the child thread, Because all other UI updates are not performed on the child thread until the child thread lifecycle ends.

UI updates are not possible in child threads. The UI update we see is actually the completion of the child thread Code, and the automatic entry to the main thread, the implementation of the child thread in the UI update function stack, which is very short in the middle of time, let everyone mistakenly think that the thread can update the UI. If the child thread is running all the time, the UI-updated function stack main thread in the child thread cannot be learned, that is, it cannot be updated. Only a very small number of UI can be directly updated by the UI. Because the current environment is acquired when the thread is opened, such as clicking on a button, the response of the button is to open up a child thread, in the child thread to the button UI update is timely, such as the above the background image, but this does not make any sense.

Wen/Wenxiaowu several life road cards Wow (Jane book author)
Original link: http://www.jianshu.com/p/7e4fbde8b53f
Copyright belongs to the author, reproduced please contact the author to obtain authorization, and labeled "Jane book author."

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.