Introduction to iOS RunLoop and iosrunloop
1. What is RunLoop?
RunLoop is a running loop.
Basic functions: 1. Keep the program running. 2. process various events in the App (such as touch events, Timer events, and Selector events). 3. save CPU resources and improve program performance: do things during work and rest during work.
Ii. Basics of RunLoop
Each thread has a unique RunLoop object.
The RunLoop of the main thread has been automatically created. The RunLoop of the sub-thread needs to be automatically created.
RunLoop is created when it is obtained for the first time and destroyed when the thread ends.
2.1 get the RunLoop object
1. Foundation
[Nsunloop currentRunLoop]; // obtain the RunLoop object of the current thread
[Nsunloop mainRunLoop]; // get the RunLoop object of the main thread
2. Core Foundation
CFRunLoopGetCurrent (); // get the RunLoop object of the current thread
CFRunLoopGetMain (); // obtain the RunLoop object of the main thread
2.2 five classes about RunLoop in Core Foundation
CFRunLoopRef, CFRunLoopModeRef, CFRunLoopSourceRef, CFRunLoopTimerRef, CFRunLoopObserverRef
2.2.1 CFRunLoopModeRef indicates the running mode of RunLoop
A RunLoop contains several modes, and each Mode contains several Source/Timer/Observer
Only one of the modes can be specified when RunLoop is started. This Mode is called CurrentMode.
If you need to switch the Mode, you can only exit the Loop, and then specify a new Mode to enter
In this way, the Source, Timer, and Observer of different groups are separated to avoid mutual influence.
- Five modes are registered by default:
- KCFRunLoopDefaultMode: the default Mode of the App. Generally, the main thread runs in this Mode.
- UITrackingRunLoopMode: interface tracking Mode, used for ScrollView tracking touch sliding, ensure that the interface sliding is not affected by other modes
- UIInitializationRunLoopMode: Enter the first Mode when the App is started.
- GSEventReceiveRunLoopMode: Internal Mode for accepting system events, which is usually unavailable
- KCFRunLoopCommonModes: This is a placeholder Mode, not a real Mode.
- CFRunLoopSourceRef is the event source (input source)
- Previous Division
- Port-Based Sources
- Custom Input Sources
- Cocoa Perform Selector Sources
- Source0: Non-Port-based
- Source1: Port-based
- CFRunLoopTimerRef is a time-based trigger.
- Basically, NSTimer
- CFRunLoopObserverRef is an observer and can monitor the status change of RunLoop.
- The following time points can be monitored:
Iii. RunLoop processing logic