First, what is Runloop?
Runloop is a running loop
Basic function: 1, keep the program running continuously 2, the processing of various events in the app (such as touch events, timer events, selector events) 3, save CPU resources, improve program performance: When doing things, the rest of the rest.
Second, the basis of runloop
Each thread has a unique Runloop object corresponding to it.
The runloop of the main thread has been automatically created, and the runloop of the child threads need to be actively created
Runloop is created at the first fetch and destroyed at the end of the thread.
2.1 Getting Runloop objects
1. Foundation
[Nsrunloop Currentrunloop]; Gets the Runloop object for the current thread
[Nsrunloop Mainrunloop]; Get the Runloop object for the main thread
2. Core Foundation
Cfrunloopgetcurrent (); Gets the Runloop object for the current thread
Cfrunloopgetmain (); Get the Runloop object for the main thread
2.2 Core Foundation 5 classes on Runloop
Cfrunloopref, Cfrunloopmoderef, Cfrunloopsourceref, Cfrunlooptimerref, Cfrunloopobserverref
2.2.1 Cfrunloopmoderef represents the operating mode of Runloop
A runloop consists of several mode, each of which contains several source/timer/observer
Each time the Runloop is started, only one mode can be specified, and this mode is called CurrentMode
If you need to switch mode, you can only exit loop and then reassign a mode to enter
This is done mainly to separate different groups of source/timer/observer, so that they do not affect each other
- The system registers 5 mode by default:
- The default mode of the Kcfrunloopdefaultmode:app, usually the main thread is run in this mode
- Uitrackingrunloopmode: interface Tracking Mode for ScrollView tracking touch swipe to ensure that the interface is not affected by other mode when sliding
- Uiinitializationrunloopmode: The first Mode entered when the APP was started, no longer used when the boot is complete
- Gseventreceiverunloopmode: Accepts internal Mode for system events, usually with no
- Kcfrunloopcommonmodes: This is a placeholder mode, not a real mode.
- Cfrunloopsourceref is the event source (input source)
- Previous sub-Methods
- Port-based Sources
- Custom Input Sources
- Cocoa Perform Selector Sources
- The present division method
- SOURCE0: Not Port-based
- Source1: Port-based
- Cfrunlooptimerref is a time-based trigger
- Basically, that's nstimer.
- CFRUNLOOPOBSERVERREF is the Observer, able to monitor the state change of Runloop
- There are several points of time that can be monitored
Third, Runloop processing logic
About IOS Runloop