The Runloop is the running loop, also called the event loop, which is essentially a dead loop. iOS a program after running, the default is to open a run cycle, there is a need to handle the operation, such as the user input events, Runloop will run itself, no need to deal with the event when the state of rest hibernation, to fully conserve CPU resources, improve program performance.
First, the role of Runloop:
1. Ensure that the program does not exit, it is accurate to ensure that the thread does not exit, usually a thread corresponding to a runloop. Once a thread's task is executed, it exits, sometimes we need a thread to be on standby and the state of the run will not exit, such as the main thread, which requires a mechanism such as runloop to accomplish such a task.
Runloop is actually just an object, just giving us a function entry to execute the runloop logic.
2. Handling various events of the app: including input sources and timing sources, the order of processing follows the FIFO-first order of the queues.
3. Save CPU resources, optimize the program: When no event can be processed, Runloop will notify the system, now there is no matter to deal with, can rest, the system can save resources to do other things, when there are events to deal with, Runloop immediately run up for processing.
Second, the relationship between Runloop and thread:
1. A thread has a unique Runloop object corresponding to it.
2.RunLoop is created on first acquisition, destroyed after the thread is run, and only Runloop (except the main path) is fetched internally to the threads.
3. The runloop of the main thread is turned on automatically, and the runloop of the child threads need to be opened manually.
When we usually set up the project, we have actually used the Runloop, the program's start-up entrance:
2345 |
int main(int argc, char * argv[]) { @autoreleasepool { return uiapplicationmain(argc, argv, nil, Nsstrin Gfromclass([appdelegate class])); }} |
Inside the Uiapplicationmain function is the runloop that initiates the main thread.
4.RunLoop is actually used to manage the thread, it can be said, no thread, there is no need for runloop existence, after the Runloop once opened, the thread will be managed, after the thread executes the task, the threads will go into hibernation, and will not exit, waiting for new tasks at any time.
5. We can get the main thread and the current thread runloop by the following code.
[Nsrunloop Currentrunloop]; [Nsrunloop Mainrunloop];
Three, runloop several mode:
There are basically five forms of mode, some of which are used internally by Apple, and are not used in our actual development:
1. Kcfrunloopdefaultmode default mode, usually the main thread runs in this mode
2, Uitrackingrunloopmode interface Tracking mode, used to track the state of ScrollView touch sliding. 3, Kcfrunloopcommonmodes placeholder, with the common tag string, a special mode; 4, Uiinitializationrunloopmode: Just start the app when the first Mode, not in use after starting. 5, Gseventreceiverunloop: Internal Mode, receiving system events
Of these,nsrunloopcommonmodes is equivalent to the combination of two modes of Nsdefaultrunloopmode and nseventtrackingrunloopmode
It is important to note that Runloop can only specify one mode at a time. And if you need to switch Mode, you can only exit the current Loop. So generally we see the timer to implement the wireless carousel function, the need to add the timer to the runloop mode to Nsrunloopcommonmodes, in order to realize the scrolling interface will not affect the rotation of the carousel , that's the reason.
A summary of runloop related knowledge