iOS Development Runloop Learning: Runloop: A brief introduction

Source: Internet
Author: User

A: A brief introduction to Runloop

#import "ViewController.h"@interfaceViewcontroller ()@end@implementationViewcontroller/** * 1:runloop and thread relationship: 1: One by one corresponding, the runloop of the main thread has been created by default, but the child threads need to be created manually: Create runloop:nsrunloop of the child thread *run = [Nsrunloop Currentrunloop];currentrunloop lazy loading, creating multiple runloop in the same child thread, the same object is returned because it is 2 of lazy load mode: There are multiple run modes in Runloop. But Runloop can only choose one mode to run, mode must have at least one timer or source 2:1. Get the main thread corresponding to the runloop:nsrunloop *mainrunloop = [Nsrunloop Mainrunloop] ;    2: Get the current thread corresponding to the runloop:nsrunloop *currentrunloop = [Nsrunloop currentrunloop]; 3:cfrunloop:1: Get the main thread corresponding to the Runloop:cfrunloopgetmain () 2: Get the Runloop:cfrunloopgetcurrent () corresponding to the current threads **/-(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *)Event{    //1. Get the Runloop of the main threadNsrunloop *mainrunloop =[Nsrunloop Mainrunloop]; //2. Get the Runloop for the current threadNsrunloop *currentrunloop =[Nsrunloop Currentrunloop]; NSLog (@"%p---%p", mainrunloop,currentrunloop);//NSLog (@ "%@", Mainrunloop); //CoreNSLog (@"%p", Cfrunloopgetmain ()); NSLog (@"%p", Cfrunloopgetcurrent ()); NSLog (@"%p", Mainrunloop.getcfrunloop); //the relationship between Runloop and Threads//one by one corresponding, the runloop of the main thread has been created, but the child threads need to be created manually[[[Nsthread alloc]initwithtarget:self selector: @selector (Run)Object: nil] start];}//There are multiple run modes in Runloop, but Runloop can only select one mode to run//there must be at least one timer or source in mode .-(void) run{//How to create a child thread corresponding to the Runloop,currentrunloop lazy loadedNSLog (@"%@", [Nsrunloop Currentrunloop]); NSLog (@"%@", [Nsrunloop Currentrunloop]); NSLog (@"Run---%@", [Nsthread CurrentThread]);}@end

#import "ViewController.h"@interfaceViewcontroller ()@end@implementationViewcontroller/** * 1:nslog (@ "%@", [Nsrunloop Currentrunloop]); Prints the current thread runloop, lazy load mode, a thread corresponding to a Runloop object, there is a return, not created, the main thread of the Runloop default creation, The runloop of a child thread needs to be created manually, [Nsrunloop currentrunloop], if multiple runloop are created in the same thread, the same Runloop object is returned. A runloop will have multiple mode modes (the system provides 5), but the runtime can only specify a runloop, if switching runloop, you need to exit the current Runloop 2: Timer nstimer problem: 1: If you create a timer with Timerwithtimeinterval, you need to manually add the timer to the Nsrunloop, and the specified run mode is default, but if there is a scrolling event, the timer stops working. WORKAROUND: Change the operating mode of the Nsrunloop, UITRACKINGRUNLOOPMODE interface tracking, this mode is when only a scrolling event occurs when the timer is turned on. If at any time the timer is turned on: nsrunloopcommonmodes, nsrunloopcommonmodes = Nsdefaultrunloopmode + uitrackingrunloopmode occupancy, label, usually added to n Srunloopcommonmodes Event Love you will be added to the Commmon tag in the run mode 3:1:scheduledtimerwithtimeinterval this method created by default added to the Nsrunloop, and set the run mode to default. 2: If you want to open nsrunloop on a child thread: you need to open it manually: Nsrunloop *currentrunloop = [Nsrunloop Currentrunloop], and when the thread is destroyed, the Currentrunloop object is destroyed.  2: In the child thread timer, need to manually add to Runloop: Do not forget to call the Run method nsrunloop *currentrunloop = [Nsrunloop currentrunloop]; The method is automatically added internally to the Runloop, and the run mode is set to the default [Nstimer scheduledtimerwithtimeinterval:2.0 target:self Selector: @selector (Run) Userinfo:nil Repeats:yes];   Open Runloop [Currentrunloop run];  /* **/-(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *)Event{//NSLog (@ "%@", [Nsrunloop Currentrunloop]); //[self timer2];//[Nsthread detachnewthreadselector: @selector (timer2) totarget:self Withobject:nil];[self timer1];}-(void) timer1{//1. Create a timerNstimer *timer = [Nstimer timerwithtimeinterval:2.0target:self selector: @selector (run) Userinfo:nil Repeats:yes]; //2. Add timer to Runloop, specify Runloop operating mode is Nsdefaultrunloopmode    /*First parameter: Timer the second parameter: Runloop operating mode*///[[Nsrunloop Currentrunloop] Addtimer:timer Formode:nsdefaultrunloopmode]; //Uitrackingrunloopmode: Interface Tracking[[Nsrunloop Currentrunloop] Addtimer:timer Formode:uitrackingrunloopmode];//nsrunloopcommonmodes = nsdefaultrunloopmode + Uitrackingrunloopmode//occupy, label, any event added to Nsrunloopcommonmodes love you will be added to the running mode of Commmon tag    /*0: <cfstring 0x10af41270 [0x10a0457b0]>{contents = "Uitrackingrunloopmode"} 2: <cfstring 0x10a065b [0x10a0457b0]>{contents = "Kcfrunloopdefaultmode"*///[[Nsrunloop Currentrunloop]addtimer:timer formode:nsrunloopcommonmodes];}-(void) timer2{Nsrunloop*currentrunloop =[Nsrunloop Currentrunloop]; //the method is automatically added to the Runloop, and the run mode is set to the default[Nstimer Scheduledtimerwithtimeinterval:2.0target:self selector: @selector (run) Userinfo:nil Repeats:yes]; //Open Runloop[Currentrunloop run];}-(void) run{NSLog (@"Run-----%@---%@", [Nsthread currentthread],[nsrunloop currentrunloop].currentmode);}@end

# # # #1. Runloop Basics

-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.

iOS Development Runloop Learning: Runloop: A brief introduction

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.