iOS developing the underlying dump run Loop--runloop
Source: Internet
Author: User
<span id="Label3"></p><p><p></p></p><p><p>http://blog.csdn.net/zc639143029/article/details/50012527</p></p><p><p></p></p><p><p></p></p><p><p></p></p>I. Runloop BASIC Concepts<p><p>Concept: the running cycle of the program, in the popular sense is the running Circle.</p></p><p><p><strong>1. Basic Role (significant)</strong><br>(1) Keep the program running continuously (why does the iOS program live without Dying)<br>(2) handle various events in the app (such as Touch event, Timer Event "nstimer", Selector event "selector Performselector 】)<br>(3) Save CPU resources, Improve program performance, have things to do things, no matter on the rest</p></p><p><p><strong>2. Important Notes</strong><br>(1) If there is no runloop, then the program will quit when it starts and nothing can be done.<br>(2) If there is a runloop, then the equivalent of a dead loop inside, can guarantee the continuous operation of the program</p></p><p><p><strong>Runloop in the 3.main function</strong><br>(1) a runloop is started inside the uiapplication function, which returns a value of type int<br>(2) This default boot Runloop is associated with the main line threads</p></p><p><p><strong>4.Runloop Objects</strong><br>(1) There are two sets of APIs to access Runloop in iOS development<br>The first Type: Foundation framework "nsrunloop"<br>The second Type: Core Foundation framework "cfrunloopref"<br>(2) both Nsrunloop and cfrunloopref represent Runloop objects, which are equivalent and can be converted to each other.<br>(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)</p></p><p><p><strong>5.Runloop and Threads</strong><br>(1) relationship between Runloop and threads: a runloop corresponds to a unique thread<br>Problem: How to keep a child thread alive<br>Answer: Open a runloop for this thread<br>(2) runloop creation: The main thread runloop has been created, the runloop of the child threads need to be created manually<br>(3) runloop Life Cycle: created on first acquisition, destroyed at end of thread<br>(4) get the main runloop of the current application (runloop corresponding to the main thread)</p></p><p><p>Method One: Nsrunloop</p></p><pre name="code" class="prettyprint"><pre name="code" class="prettyprint"><code class="hljs fix has-numbering"><span class="hljs-attribute"> NSRunLoop * runloop1 =<span class="hljs-string"> [NSRunLoop mainRunLoop];</span></span></code></pre></pre> <ul class="pre-numbering"> <ul class="pre-numbering"> <li>1</li> </ul> </ul><p><p>Method Two: Cfrunloopref</p></p><pre name="code" class="prettyprint"><pre name="code" class="prettyprint"><code class="hljs fix has-numbering"><span class="hljs-attribute"> CFRunLoopRef runloop2 =<span class="hljs-string"> CFRunLoopGetMain();</span></span></code></pre></pre> <ul class="pre-numbering"> <ul class="pre-numbering"> <li>1</li> </ul> </ul><p><p>(5) <strong>note:</strong> Open a sub-thread to create a runloop, not created by the Alloc init method, but directly by calling the Currentrunloop method to create, which itself is a lazy load.</p></p>Two. Runloop Operating principle<p><p>1. Apple's official operating schematics<br></p></p><p><p>Diagram of 2.Runloop and related classes<br></p></p><p><p>Runloop Five related classes</p></p> <ul> <li><li>Cfrunloopref</li></li> <li><li>Cfrunloopmoderef "runloop Operation mode"</li></li> <li><li>Cfrunloopsourceref "runloop Event Source to process"</li></li> <li><li>Cfrunlooptimerref "timer event"</li></li> <li><li><p>CFRUNLOOPOBSERVERREF "runloop Observer (listener)"</p><p>the <strong>Following is a cast of five related classes</strong> :</p></li></li> <li><li> <p> <strong> cfrunloopref </strong> <br> (1) cfrunloopmoderef represents runloop's operating mode <br> (2) A runloop can have multiple mode, a mode inside can have multiple source\observer\timer and so on <br> (3) each time the Runloop starts, only one mode is specified, This mode is called the current mode of the runloop; <br> (4) if you need to switch mode, you can only exit the current runloop, and then reassign a mode into the <br> (5) This is mainly to split the different sets of timers, etc., Let them not be affected by each other <br> (6) the system registers 5 mode by default; <br> First mode: kcfrunloopdefaultmode:app default mode, usually the main thread is run in this mode <br> Second Mode: Uitrackingrunloopmode: interface tracking mode, for ScrollView tracking touch sliding, to ensure that the interface is not affected by other modes of mode <br> the third mode: Uiinitializationrunloopmode: The first mode entered when the APP was started, the is no longer used when the boot is complete; <br> Fourth mode: gseventreceiverunloopmode: accepts internal system events mode, usually less than <br> Fifth mode: kcfrunloopcommonmodes: This is a placeholder mode, not a real mode </p> </li></li> <li><li><p><strong>Cfrunlooptimerref</strong><br>(1) Runloop A mode will be selected when a mode is selected, and other modes will not participate when one is Chosen. You can add multiple Nstimer to a mode, which means that you can specify in what mode it will run when you create Nstimer.<br>(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.<br>(3) related Code/-(void) timer2 {//nstimer called the Scheduledtimer method, then it is automatically added to the current runloop, and Runloop operation mode Kcfrunloopdefaultmode</p></li></li> </ul><pre name="code" class="prettyprint"><pre name="code" class="prettyprint"><code class="hljs objectivec has-numbering">NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:<span class="hljs-number">2.0 target:<span class="hljs-keyword">self selector:<span class="hljs-keyword">@selector(run) userInfo:<span class="hljs-literal">nil repeats:<span class="hljs-literal">YES];[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];</span></span></span></span></span></code></pre></pre> <ul class="pre-numbering"> <ul class="pre-numbering"> <li>1</li> <li>2</li> </ul> </ul> <ul> <ul> <li><p><strong>Cfrunloopsourceref</strong><br>(1) is the event source is the input source, there are two types of classification mode;<br>One is classified according to Apple's official Document.<br>The other is a function-based call stack for partitioning (source0 and source1).<br>(2) the specific classification situation</p><p>* Previous Sub-methods<br>Port-based Sources<br>Custom Input Sources<br>Cocoa Perform Selector Sources</p><p>* The present Division method<br>Source0: not port-based<br>Source1: port-based</p><p>(3) the function call stack of a method can be viewed by breaking point</p></li> <li><p>Cfrunloopobserverref<br>(1) Cfrunloopobserverref is the observer, able to monitor the state change of Runloop<br>(2) How to monitor</p> <blockquote> <p>Create a RUNLOOP listener cfrunloopobserverref observer =<br>Cfrunloopobservercreatewithhandler (cfallocatorgetdefault (), kcfrunloopallactivities,<br>YES, 0, ^ (cfrunloopobserverref observer, cfrunloopactivity Activity) {<br>NSLog (@ "monitor runloop status change-%zd", activity); }); Add a listener for Runloop cfrunloopaddobserver (cfrunloopgetcurrent (), observer,<br>kcfrunloopdefaultmode); Cfrelease (observer);</p> </blockquote></li> </ul> </ul><p><p>(3) Status of monitoring</p></p> <blockquote> <blockquote> <p>typedef cf_options (cfoptionflags, Cfrunloopactivity) {<br>Kcfrunloopentry = (1UL << 0),//coming into Runloop<br>Kcfrunloopbeforetimers = (1UL << 1),//will be processed Nstimer<br>kcfrunloopbeforesources = (1UL << 2),//will be processed sources<br>kcfrunloopbeforewaiting = (1UL << 5),//going into hibernation<br>kcfrunloopafterwaiting = (1UL << 6),//just wake-up from hibernation<br>Kcfrunloopexit = (1UL << 7),//imminent exit Runloop<br>Kcfrunloopallactivities = 0x0fffffffu//all State changes};</p> </blockquote> </blockquote>Three. Runloop Run Logic<p><p>The following is the user collation of the running logic diagram:<br></p></p><p><p>We can make a summary of the following running Logic:</p></p> <ol> <li><li><p>A thread corresponding to a runloop, the main thread of the Runloop is the start of the program, the default is to create a runloop, created after it will add some default mode, each mode will have a lot of source/timer/observer, add these patterns , observer will listen to the main thread of the runloop, enter the runloop, began to deal with the event, first processing timer, and then processing SOURCE0,SOURCE0 processing after the source1, when all these events are processed repeatedly, If there is no event, then Runloop will go to sleep, when the user triggered a new event, will wake up the runloop, wake up Runloop back to the second step, re-processing new timer, new source0, new source1, processing after the sleep, has repeatedly , when we put the program closed or strong back, this time observer will listen to Runloop exit.</p></li></li> <li><li><p>Observer is monitoring the Runloop State.</p></li></li> <li><li><p>timer/source/<br>CFRUNLOOPREF Create a timer must be added to Runloop to execute, add the time to specify the mode Defaurce mode, do not do anything to the program timer will run in the background, When I do the Runloop mode will switch from the default mode to the other mode, if I operate scrollerview it will switch from default mode to tracking mode<br>And Roonloop can only execute one pattern at a Time.<br>When you specify that Comment is only a tag, default, and trace when you create a timer, the timer will choose the default and tracking mode with the trigger Mode.</p></li></li> <li><li><p>Source:source generally do not operate, the first is based on a custom, the second is port-based, and the third is based on Performselect<br>Use the function call stack to classify the source: divided into Source0 source1:</p> <ul> <li><p>Source0 is a non-port-based operation that is triggered manually by the user, such as Touch and Swipe.</p></li> <li><p>Source1 is a number of port-triggered events inside the system</p></li> <li><p>The runloop of the child threads need to be manually created and need to be manually opened</p></li> </ul></li></li> </ol><p><p>5. Automatically release the pool, when to create and release?<br>(1) first creation: The corresponding state is created when the runloop is entered = Kcfrunloopentry<br>(2) the last Exit is the corresponding state at the time of runloop exit = Kcfrunloopexit<br>(3) other creation and release<br>Each time you go to sleep, the pool is automatically released before you create a new<br>When you are about to go to sleep, release the last created Auto-release pool before creating a new release pool</p></p><p><p>iOS developing the underlying dump run Loop--runloop</p></p></span>
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