IOS RunLoop Analysis 2. iosrunloop Analysis
In the previous article, we talked about several runloop modes. What do we need to do in the mode ???
There are three modules in the Mode:
Event Source (Input Source) Source
Source:
By Official Document
Port-Based
Custom Input
Cocoa Perform Selector
By function call stack and Source
Source0: Non-Port-based
Source1: Port-based, communicates with other threads through the kernel, accepts and distributes system events.
(Nothing is too useful here. I will not introduce the remaining Source concepts. You can check them elsewhere)
Observer
The observer monitors the execution status in the runloop.
The following statuses are available:
Status of the listener:
KCFRunLoopEntry = (1UL <0), 1
KCFRunLoopBeforeTimers = (1UL <1), 2
KCFRunLoopBeforeSources = (1UL <2), 4
KCFRunLoopBeforeWaiting = (1UL <5), 32
KCFRunLoopAfterWaiting = (1UL <6), 64
KCFRunLoopExit = (1 ul <7), 128
KCFRunLoopAllActivities = 0x0FFFFFFFU
We can choose to listen to different States for different operations
In the following code, all the statuses are displayed on the day.
/// ViewController. m // CX RunLoop analysis // Created by ma c on 16/3/29. // Copyright©2016 xubaoaichiyu. all rights reserved. // # import "ViewController. h "@ interface ViewController () @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad];}-(void) touchesBegan :( NSSet <UITouch *> *) touches withEvent :( UIEvent *) event {// Add the status kCFRunLoopEntry = (1UL <0), 1 kCFRunLoopBeforeTimers = (1UL <1 ), 2 kCFRunLoopBeforeSources = (1UL <2), 4 kCFRunLoopBeforeWaiting = (1UL <5), 32 ul = (1UL <6), 64 kCFRunLoopExit = (1UL <7 ), 128 rows = 0x0FFFFFFFU */CFRunLoopObserverRef observe = rows (CFAllocatorGetDefault (), rows, YES, 0, ^ (CFRunLoopObserverRef observer, CFRunLoopActivity) {NSLog (@ "Listening to runloop -- % zd", activity) ;}); // Add the observer CFRunLoopAddObserver (CFRunLoopGetCurrent (), observe, kcfrunloopdefadefamode);} @ end
The demo effect is:
After careful observation, it is not difficult to find that when I am not operating at the end of the operation, the log stops printing.
Why ??? When you observe all the statuses, you will know that it is in sleep state.
To prove this point of view, we have been clicking the screen to test it.
(The 32 output after the test result is not logical, because I am always * quick * clicking .)
(Normally, 64 or so should occur)
Because no other status exists, it remains in sleep state (the specific process will be introduced in the next article)
Mastering this will be of great benefit to development.
Timer NSTimer
Since the first article used a timer to briefly introduce runloop, we will not pursue prosecution here.
If you do not see the first article, you can click the previous article at the end of the article.