“This is the third day of my participation in the Gwen Challenge in November. Check out the details: The last Gwen Challenge in 2021.”
The application of multithreading in development
What is the main thread
A thread that starts by default after an iOS application runs, called the main thread or UI thread
Main thread action
- The page is displayed and refreshed
- Handle UI events, such as click events, scroll events, drag events, and so on
Note the main thread usage
-
Do not put time-consuming operations in the main thread
-
Time-consuming operations can jam the main thread and seriously affect fluency
If put time-consuming operation in the main thread, when a user in 5 seconds, click on the button, the operation must be serialized because the thread, the click event will at this time in 10 seconds after lengthy operations, until 10 seconds after the time-consuming operation, click on the button to perform event, this can cause the UI to jam phenomenon, the following figure:
If the time-consuming operation is placed in the child thread, the main thread and the child thread will do the time-consuming operation at the moment. When the user clicks the button at the fifth second, the child thread will do the time-consuming operation, and the main thread responds to the interface operation, so the UI will not be stuck. Therefore, the time-consuming operation should be placed in the background thread of the child thread, as shown in the figure:
Multithreaded implementation scheme
Technical solution | Introduction to the | language | Thread life cycle | Use frequency |
---|---|---|---|---|
Pthread | A set of general-purpose multithreading API, suitable for Unix, Linux, Windown and other systems. Cross-platform, portable and difficult to use |
The C language | Programmer management | Almost no |
NSThread | Use more object-oriented. Easy to use, direct manipulation of thread objects |
The Object – the C language | Programmer management | Occasionally use |
GCD | Designed to replace threading technologies such as NSThreads Take full advantage of multi-core equipment |
The C language | Automatic management | Often use |
NSOperation | Based on GCD (bottom is GCD), some more simple and practical functions than GCD, use more object-oriented | The Object – the C language | Automatic management | Often use |