reference

Ke.qq.com/course/3145…

RunLoop basic functions

1. Keep the program running.

2. Handle various events in App, such as touch events and timer events.

3. Save CPU resources and improve program performance: work when the work, rest when the rest.

RunLoop object

In iOS, RunLoop has two sets of apis to use, one set of open source C language CFRunLoopRef, and one set of OC NSRunLoop, NSRunLoop is based on a layer of OC packaging of CFRunLoopRef.

NSRunLoop * RunLoop = [NSRunLoop currentRunLoop]; CFRunLoopRef runloop = CFRunLoopGetCurrent(); NSRunLoop * RunLoop = [NSRunLoop mainRunLoop]; CFRunLoopRef runloop = CFRunLoopGetMain();Copy the code

RunLoop in relation to threads

1. Each thread has a unique RunLoop object.

2. Runloops are stored in a global Dictionary with threads as keys and runloops as values.

3. There is no RunLoop object when the thread is first created. The RunLoop is created when it first gets it.

4. The RunLoop is destroyed at the end of the thread.

5. The RunLoop of the main thread is automatically obtained, and RunLoop is not enabled for the child thread by default.

RunLoop,

1. A Runloop contains several modes, each of which contains several Source0/ Source1/ Timer/ Observer. Switching Mode does not cause the program to exit.

2. Only one Mode can be selected as currentMode when Runloop starts.

3. If you need to switch Mode, you can only exit the current Mode and select another Mode to enter. Source0, Source1, Timer, and Observer of different groups can be separated from each other.

4. If there is no Source0/ Source/ Timer/ Observer in the Mode, Runloop will exit immediately.

Mode,

Mode Internal Source0/ Source1/ Timer/ Observer resolution

1 > Source0:

Touch event handling

2 > Source1:

Port based communication between threads, system event capture, click events are captured by Source1, distributed to Source0 for processing

3 > Timers:

Timer NSTimer, delay operation performSelector: withobject: Afterdelay:

4 > Observers:

Listen for Runloop status, UI refresh, automatically release pool Autorelease pool

RunLoop runs the logic

The internal loop processes blocks, sources, timers, etc., sleeps when the process is complete, and continues processing when the message wakes up.

RunLoop application scenarios

1. Control the thread lifecycle

2. Solve the problem that the timer stops working when sliding

3. Monitoring applications are stuck

4. Performance optimization, etc

__block NSInteger count = 0; NSTimer * timer = [NSTimer timerWithTimeInterval: 1.0 repeats: YES block: ^ (NSTimer * _Nonnull timer) {NSLog (@"%ld",(long)++count);
}];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
Copy the code

supplement

1. If the memory address of Runloop needs to be printed, the correct address value printed by CFRunLoopRef is the correct address value, and the Runloop address value printed by NSRunLoop is wrapped.

2.RunLoop hibernation is kernel hibernation and does not consume CPU resources.

3. If this article violates privacy or other things, please contact me, I will rectify or delete in the first time.