Several timers in iOS

Source: Internet
Author: User
<span id="Label3"></p><p><p>There are three methods listed here: nstimer, cadisplaylink, GCD.</p></p><p><p><span style="color: #008000;">first, Nstimer</span></p></p><p><p>(1) Create</p></p><p><p>Nstimer *timer = [nstimer scheduledtimerwithtimeinterval:1.0 target:self selector: @selector (action:) Userinfo:nil repeats:no]; TimerInterval the time to wait before executing, 1.0 means one second after execution</p></p><p><p>(2) Manual Start</p></p><p><p>-(void) fire;</p></p><p><p>(3) Stop: After invoking the Create method, the target Object's counter will be incremented by 1 until the execution is complete and automatically minus 1. If it is a loop execution, it must be closed manually, otherwise the release method may not be Performed.</p></p><p><p>-(void) invalidate;</p></p><p><p>(4) there is a delay</p></p><p><p>The timing of the actual trigger event for a timer, whether it be a one-time or periodic, will be related to the Runloop and Runloop mode being added, and if the runloop is performing a continuous operation, the timer will be Delayed. A repetitive timer encounters this situation, and if the delay exceeds a period, it is executed immediately after the delay and continues with the period specified earlier.<br><br>Must join Runloop<br>Using the creation method above, the timer is automatically added to the Mainrunloop Nsdefaultrunloopmode. If you create a timer using the following method, you must manually join the Runloop:<br>[[nsrunloop mainrunloop] Addtimer:timer formode:nsdefaultrunloopmode];</p></p><p><p></p></p><p><p><span style="color: #008000;">second, Cadisplaylink</span></p></p><p><p>(1) use</p></p><pre><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Create</span></span>+ (cadisplaylink *) displaylinkwithtarget: (<span style="color: #0000ff;"><span style="color: #0000ff;">ID</span></span><span style="color: #000000;"><span style="color: #000000;">) Target selector: (sel) sel;</span></span>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>) addtorunloop: (nsrunloop *) runloop formode: (nsstring *<span style="color: #000000;"><span style="color: #000000;">) mode;</span></span>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>) removefromrunloop: (nsrunloop *) runloop formode: (nsstring *<span style="color: #000000;"><span style="color: #000000;">) mode;</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Stop</span></span>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">) invalidate; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Set Timer</span></span>Cadisplaylink *link =<span style="color: #000000;"><span style="color: #000000;">[cadisplaylink displaylinkwithtarget:self selector: @selector (action)]; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Turn on Timer</span></span><span style="color: #000000;"><span style="color: #000000;">[link addtorunloop:[nsrunloop mainrunloop] formode:nsdefaultrunloopmode];</span></span>-(<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">) Stoplink {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Turn off the timer</span></span><span style="color: #000000;"><span style="color: #000000;">[self.link invalidate]; Self.link</span></span>=<span style="color: #000000;"><span style="color: #000000;">nil;} (</span></span><span style="color: #800080;"><span style="color: #800080;">2</span></span><span style="color: #000000;">the <span style="color: #000000;">feature Cadisplaylink is a timer class that allows us to draw specific content to the screen at a frequency that is synchronized with the screen refresh Rate. Once the Cadisplaylink is registered to Runloop in a specific mode, Runloop sends a specified selector message to the target specified by Cadisplaylink whenever the screen displays the end of the content Refresh. The corresponding selector of the Cadisplaylink class is called Once. So typically, follow the iOS device screen refresh rate 60 times</span></span>/<span style="color: #000000;"><span style="color: #000000;">seconds, the screen refresh frequency of iOS devices is fixed, Cadisplaylink will normally be called at the end of each refresh, and the accuracy is quite high. however, if the calling method is more time-consuming than the screen refresh cycle, it can cause several callback invocation opportunities to be Skipped. If the CPU is too busy, the screen cannot be guaranteed 60 times</span></span>/<span style="color: #000000;"><span style="color: #000000;">the refresh rate of seconds causes the chance to skip several calls to the callback method, depending on how busy the CPU is. (</span></span><span style="color: #800080;"><span style="color: #800080;">3</span></span><span style="color: #000000;">The use of the <span style="color: #000000;">scene from the principle can be seen, cadisplaylink suitable for the interface of the continuous repainting, such as video playback need to constantly get the next frame for the interface rendering. three, GCD way to execute once</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Double</span></span>Delayinseconds =<span style="color: #800080;"><span style="color: #800080;">2.0</span></span><span style="color: #000000;"><span style="color: #000000;">;d ispatch_time_t poptime</span></span>= Dispatch_time (dispatch_time_now, Delayinseconds * nsec_per_sec); Dispatch_after (poptime, dispatch_get_main_queue (), ^ (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>){<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Execute event});</span></span><span style="color: #000000;"><span style="color: #000000;">Repeat Nstimeinterval period</span></span>=<span style="color: #800080;"><span style="color: #800080;">1.0</span></span>;<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">set the time interval</span></span><span style="color: #000000;"><span style="color: #000000;">dispatch_queue_t Queue</span></span>= Dispatch_get_global_queue (dispatch_queue_priority_default,<span style="color: #800080;"><span style="color: #800080;">0</span></span><span style="color: #000000;"><span style="color: #000000;">);d ispatch_source_t _timer</span></span>= Dispatch_source_create (dispatch_source_type_timer,<span style="color: #800080;"><span style="color: #800080;">0</span></span>,<span style="color: #800080;"><span style="color: #800080;">0</span></span><span style="color: #000000;"><span style="color: #000000;">, queue);d ispatch_source_set_timer (_timer, dispatch_walltime (NULL,</span></span><span style="color: #800080;"><span style="color: #800080;">0</span></span>), Period * nsec_per_sec,<span style="color: #800080;"><span style="color: #800080;">0</span></span>);<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">executes per second</span></span>Dispatch_source_set_event_handler (timer, ^<span style="color: #000000;"><span style="color: #000000;">{NSLog (</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">Wakeup</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">); Dispatch_source_cancel (timer); NSLog (</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">end:%@</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">, [formatter stringfromdate:[nsdate date]]); }); Dispatch_source_set_cancel_handler (timer,</span></span>^<span style="color: #000000;"><span style="color: #000000;">{NSLog (</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">Cancel</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">); NSLog (</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">end:%@</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">, [formatter stringfromdate:[nsdate date]]); });</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Turn on Timer</span></span><span style="color: #000000;"><span style="color: #000000;">Dispatch_resume (_timer);</span></span></pre><p><p><span style="color: #008000;">V. <code>CADisplayLink</code> <code>NSTimer</code> what is the difference</span></p></p><p><p>The <span id="6_nwp">screen refresh frequency of the iOS device is fixed and is <code>CADisplayLink</code> normally called at the end of each flush, with very high accuracy.<br><code>NSTimer</code>The accuracy is low, such as when the <code>NSTimer</code> trigger time, <code>runloop</code> If in the blocking state, the trigger time will be postponed to the next <code>runloop</code> cycle. and <code>NSTimer</code> added <span style="color: #ff0000;"> <code>tolerance</code> </span> properties that allow the user to set the delay range for the time that can be tolerated.<br><code>      CADisplayLink</code>The use of a relatively exclusive, suitable for the UI of the constant repainting, such as custom animation engine or video playback <span id="5_nwp">of the Rendering. <code>NSTimer</code>use a wide range of applications, a variety of tasks that require a single or cyclic timing processing can be used. The advantage of using the ui-related animations or display content <code>CADisplayLink</code> <code>NSTimer</code> is that we don't need to be particularly concerned about the refresh rate of the screen, because it's in sync with the screen refresh Itself. </span></span></p></p><p><p></p></p><p><p>Several timers in iOS</p></p></span>

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.