During iOS development, I always knew that updating the UI needed to be in the main thread, but I didn't think much of why I was in the main thread, or why I couldn't update the UI in a child thread.
Ensuring thread safety on a large framework like Uikit is a significant task that can bring huge costs. Uikit is not thread-safe, if the same background image 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 iterates through the search for a subview, but removes the subview from another thread, can cause confusion. Apple has made it possible for most of the drawing methods and classes such as Uicolor to be thread-safe, or it is recommended that UI actions be guaranteed in the main thread.
In fact, in a child thread, if you want to update the other UI, you must wait until the child thread finishes running, and the UI update to the button that responds to the user's click is timely, no matter whether he is updating on the main thread or the child thread, it doesn't mean much. Because all other UI updates in a child thread wait until the end of the child thread's life cycle.
In a child thread is not UI update, we see the UI update is actually the child thread code execution is complete, and automatically into the main thread, executing the UI update in the child thread function stack, the middle of the time is very short, let everyone mistakenly think that the sub-thread can update the UI. If the child thread is running all the time, the main thread of the function stack in the UI update in the child thread is not known and cannot be updated. Only a very small number of UI can be directly updated UI, because the thread will get the current environment, such as clicking on a button, this button response is to open a sub-thread, UI updates to the button in the child thread is timely, such as the above the background image, but it doesn't make any sense
If the UI updates on a child thread may occur: A flashback may occur, or an animation exception may be thrown, such as the overall animation effect disappears including pop push
Conclusion:
1, in the child thread is not the UI update, and can update the result is just a phantom: because the child thread code execution is complete, and automatically into the main thread, executing the UI updates in the child thread function stack, the middle of the time is very short, let everyone mistakenly think that the sub-thread can update the UI. If a child thread is running, the main thread of the function stack in the UI update in the child thread is not known, and cannot be updated
2, only a very small number of UI can, because the thread will get the current environment, such as click on a button, this button response is to open a sub-thread, in the child thread UI update is timely, such as the title, change the background, but it doesn't make any sense
Speaking of threads, you have to mention Nsthread class Nsoperationqueue and GCD
So we can use the method of threading to improve the speed of the program, basically we create sub-threading method roughly divided into three kinds: ...
About threading updates to the UI by child threads