1.Runloop Basic Knowledge
-1.1 literal meaning
A Run cycle
B Running Circle
-1.2 basic functions (significant effect)
A keep the program running continuously (why does the iOS program live without dying)
b handles various events in the app (such as Touch event, timer event "Nstimer", selector event selector performselector 】)
C Save CPU resources, improve program performance, have things to do things, no matter on the rest
-1.3 Important Notes
(1) If there is no Runloop, then the program will quit when it starts and nothing can be done.
(2) If there is a runloop, then the equivalent of a dead loop inside, can guarantee the continuous operation of the program
(2) Runloop in main function
A runloop is launched inside the UIApplication function.
The function returns a value of type int
b This default boot Runloop is associated with the mainline threads
-1.4 Runloop Object
(1) There are two sets of APIs to access Runloop in iOS development
A.foundation Frame "Nsrunloop"
B.core Foundation Framework "CFRUNLOOPREF"
(2) both Nsrunloop and Cfrunloopref represent Runloop objects, which are equivalent and can be converted to each other.
(3) Nsrunloop is a cfrunloopref-based OC packaging, so to understand the RUNLOOP internal structure, we need to study the CFRUNLOOPREF level of the API (Core Foundation layer)
-1.5 Runloop Reference
"' OBJC
(1) Official Apple document
https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Multithreading/RunLoopManagement/ Runloopmanagement.html
(2) Cfrunloopref Open Source code:
http://opensource.apple.com/source/CF/CF-1151.16/
```
-1.6 Runloop and threading
1.Runloop and thread relationships: a runloop corresponds to a unique thread
Problem: How to keep a child thread alive
Answer: Open a runloop for this thread
2.Runloop creation: The main thread runloop has been created, the runloop of the child threads need to be created manually
3.Runloop life cycle: Created on first fetch, destroyed at end of thread
-1.7 Get Runloop Object
"' OBJC
1. Get the current Runloop object
Nsrunloop
Nsrunloop * Runloop1 = [Nsrunloop currentrunloop];
Cfrunloopref
Cfrunloopref runloop2 = Cfrunloopgetcurrent ();
2. Get the main runloop of the current application (Runloop for the main thread)
Nsrunloop
Nsrunloop * Runloop1 = [Nsrunloop mainrunloop];
Cfrunloopref
Cfrunloopref runloop2 = Cfrunloopgetmain ();
3. Note: Open a sub-thread to create Runloop, not created by the Alloc init method, but directly by calling the Currentrunloop method to create, which itself is a lazy load.
4. In a child thread, Runloop is not created inside a child thread if it is not actively acquiring runloop. You can download Cfrunloopref source code, search for _cfrunloopget0, and view the codes.
The 5.Runloop object is stored using a dictionary, and key is the corresponding thread value for the runloop of that thread.
```
-1.8 Runloop Related categories
(1) Runloop operation schematic diagram
! [PNG] (2.png)
(2) Five related classes
A.cfrunloopref
B.cfrunloopmoderef "Runloop operation mode"
C.cfrunloopsourceref "Runloop Event Source to process"
D.cfrunlooptimerref "Timer Event"
E.CFRUNLOOPOBSERVERREF "Runloop Observer (Listener)"
(3) diagram between Runloop and related classes
! [PNG] (1.png)
(4) Runloop to run up, it must have a mode inside it, this mode must have Source\observer\timer, at least one of them.
-Cfrunloopmoderef
1.CFRUNLOOPMODEREF represents the operating mode of the Runloop
2. A runloop can have multiple mode, a mode inside can have multiple source\observer\timer and so on
3. Each time the Runloop starts, only one mode can be specified, and this mode is called the current mode of the Runloop
4. If you need to switch mode, you can only exit the current Runloop and then re-specify a mode to enter
5. This is done mainly to split the different groups of timers, etc., so that they are not affected by each other
6. The system registers 5 mode by default
The default mode of the A.kcfrunloopdefaultmode:app, usually the main thread is run in this mode
B.uitrackingrunloopmode: interface Tracking Mode for ScrollView tracking touch swipe to ensure that the interface is not affected by other mode when sliding
C.uiinitializationrunloopmode: The first Mode entered when the APP was started, no longer used when the boot is complete
D.gseventreceiverunloopmode: Accepts internal Mode for system events, usually with no
E.kcfrunloopcommonmodes: This is a placeholder mode, not a real mode.
-Cfrunlooptimerref
(1) Nstimer related code
"' OBJC
/*
Description
(1) Runloop will select a mode when one is started and the other mode will not be bird when one mode is selected. You can add multiple Nstimer to a mode, which means that you can specify in what mode it will run when you create Nstimer.
(2) It is a time-based flip-flop, which means that time is up. I trigger an event that triggers an action. Basically, that's nstimer.
(3) Related code
*/
-(void) Timer2
{
Nstimer calls the Scheduledtimer method, it is automatically added to the current Runloop, and the Runloop run mode Kcfrunloopdefaultmode
Nstimer *timer = [Nstimer scheduledtimerwithtimeinterval:2.0 target:self selector: @selector (Run) Userinfo:nil repeats: YES];
Change mode
[[Nsrunloop Currentrunloop] Addtimer:timer formode:nsrunloopcommonmodes];
}
-(void) timer1
{
[Nstimer scheduledtimerwithtimeinterval:2.0 target:self selector: @selector (run) Userinfo:nil Repeats:yes];
Nstimer *timer = [Nstimer timerwithtimeinterval:2.0 target:self selector: @selector (run) Userinfo:nil Repeats:yes];
Timer added to Uitrackingrunloopmode mode, once the runloop switch mode, then the timer does not work
[[Nsrunloop Currentrunloop] Addtimer:timer Formode:uitrackingrunloopmode];
Timer added to Nsdefaultrunloopmode mode, once the runloop switch mode, then the timer does not work
[[Nsrunloop Currentrunloop] Addtimer:timer Formode:nsdefaultrunloopmode];
Placeholder mode: Common modes Tag
Patterns marked as common modes Kcfrunloopdefaultmode Uitrackingrunloopmode
[[Nsrunloop Currentrunloop] Addtimer:timer formode:nsrunloopcommonmodes];
NSLog (@ "%@", [Nsrunloop Currentrunloop]);
}
-(void) run
{
NSLog (@ "---run---%@", [Nsrunloop Currentrunloop].currentmode);
}
-(ibaction) Btnclick {
NSLog (@ "---btnclick---");
}
```
(2) Timer in GCD
"' OBJC
0. Create a queue
dispatch_queue_t queue = dispatch_get_global_queue (0, 0);
1. Create a GCD timer
/*
First parameter: Description This is a timer
The fourth parameter: GCD's callback task is added to that queue, and if it is the home row, it executes on the main thread.
*/
dispatch_source_t timer = dispatch_source_create (dispatch_source_type_timer, 0, 0, queue);
2. Set the timer start time, interval time and accuracy
Set start time, call after three seconds
dispatch_time_t start = Dispatch_time (dispatch_time_now,3.0 *nsec_per_sec);
Set the time interval for timer work
uint64_t Intevel = 1.0 * NSEC_PER_SEC;
/*
First parameter: which timer to set
Second parameter: The start time of the timer Dispatch_time_now indicates the current start
Third parameter: Interval between timer invocation methods
The fourth parameter: The accuracy of the timer, if the 0 is the most accurate way to calculate, if the number is greater than 0, it means that the timing switch I can receive the error in the range of the value, usually pass 0
The meaning of this parameter: the performance of the program can be improved appropriately
Note: The time in the GCD timer is measured in nanoseconds (interview)
*/
Dispatch_source_set_timer (timer, start, intevel, 0 * nsec_per_sec);
3. Set the callback method after the timer is turned on
/*
First parameter: which timer to set
Second parameter: callback block
*/
Dispatch_source_set_event_handler (Timer, ^{
NSLog (@ "------%@", [Nsthread CurrentThread]);
});
4. Execution Timer
Dispatch_resume (timer);
Note: dispatch_source_t is essentially the OC class, where it is a local variable and requires a strong reference
Self.timer = timer;
```
-Cfrunloopsourceref
1. The event source is the input source, there are two classification modes;
One is classified according to Apple's official document.
The other is a function-based call stack for partitioning (SOURCE0 and Source1).
2. Specific classification situation
(1) Previous sub-law
Port-based Sources
Custom Input Sources
Cocoa Perform Selector Sources
(2) The present division method
SOURCE0: Not Port-based
Source1: Port-based
3. You can view the function call stack of a method by breaking the point
-Cfrunloopobserverref
(1) CFRUNLOOPOBSERVERREF is the Observer, able to monitor the state change of Runloop
(2) How to monitor
"' OBJC
Create a Runloop listener
Cfrunloopobserverref observer = Cfrunloopobservercreatewithhandler (Cfallocatorgetdefault (), Kcfrunloopallactivities, YES, 0, ^ (cfrunloopobserverref Observer, cfrunloopactivity activity) {
NSLog (@ "Monitor runloop status Change---%zd", activity);
});
Add a listener to Runloop
Cfrunloopaddobserver (Cfrunloopgetcurrent (), observer, Kcfrunloopdefaultmode);
CFRELEASE (Observer);
```
(3) Status of monitoring
"' OBJC
typedef cf_options (Cfoptionflags, cfrunloopactivity) {
Kcfrunloopentry = (1UL << 0),//coming into Runloop
Kcfrunloopbeforetimers = (1UL << 1),//will be processed Nstimer
Kcfrunloopbeforesources = (1UL << 2),//will be processed sources
kcfrunloopbeforewaiting = (1UL << 5),//going into hibernation
kcfrunloopafterwaiting = (1UL << 6),//Just wake-up from hibernation
Kcfrunloopexit = (1UL << 7),//Imminent exit Runloop
Kcfrunloopallactivities = 0X0FFFFFFFU//All state changes
};
```
-1.9 Runloop Run logic
-
! [PNG] (3.png)
--------------------
! [PNG] (4.png)
# # # #2. Runloop applications
Nstimer
ImageView Display
Performselector
Resident thread
Auto Free Pool
Runloop detailed explanations in IOS