Three types of timers in IOS Nstimer, Cadisplaylink, GCD

Source: Internet
Author: User

First,Nstimer


1. Create a method

Nstimer *timer = [Nstimer scheduledtimerwithtimeinterval:1.0 target: self selector:@selector (action:) UserInfo:nil repeats:NO];

TimerInterval: the time to wait before executing. For example, set to 1.0, which represents 1 seconds after the execution method

target: The object that needs to execute the method.

Selector: The method to be executed

Repeats: whether to cycle


2. Release method

[Timer invalidate];

Note : After calling the Create method, the target object's counter will be incremented by 1until the execution is complete, automatically minus 1. If it is a loop execution, it must be closed manually, otherwise the release method may not be performed.


3. features

There is a delay, whether it is a one-time or periodic timer , the actual triggering of the event, will be joined with the Runloop and runloop Mode , if this runloop is performing a continuous operation, thetimer 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.


must join Runloop


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:

Nstimer *timer = [Nstimer timerwithtimeinterval:5 target: self selector:@selector( timeraction) UserInfo:nil repeats:YES];

[[Nsrunloop Mainrunloop] Addtimer:timer Formode:nsdefaultrunloopmode];


Second, Cadisplaylink


1. Create a method

self. DisplayLink = [Cadisplaylink displaylinkwithtarget: self selector:@selector( Handledisplaylink:)];

[self. DisplayLink addtorunloop:[nsrunloop Currentrunloop] formode:nsdefaultrunloopmode];


2. Stop method

[self. DisplayLink invalidate];

self. DisplayLink = Nil;

* * when the Cadisplaylink object is add to runloop ,selector can be called periodically, similar to repeating Nstimer When the invalidate operation is performed , the Cadisplaylink object is removed from the Runloop ,and the selector call is stopped, similar to the Invalidate method of Nstimer . **


3. features

Called when the screen refreshes

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, therunloop will be assigned to the cadisplaylink when the screen displays the end of the content refresh. target sends a specified selector message once, and the corresponding selector of the Cadisplaylink class is called once. So typically, follow the iOS device screen refresh rate of up to a second


Delay

The screen refresh rate for IOS devices is fixed, andCadisplaylink is normally called at the end of each flush, with very high accuracy. 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 to guarantee The refresh rate of the screen for a number of times / seconds, it will cause the opportunity to skip several calls to the callback method, depending on how busy the CPU is.


Usage Scenarios

It can be seen from the principle thatcadisplaylink is suitable for the continuous redrawing of the interface, such as video playback need to constantly get the next frame for the interface rendering.


4. Important attributes

Frameinterval

The value of the nsinteger type, used to set the interval for how many frames are called once selector method, the default value is 1, which is called once per frame.


Duration

The Cftimeinterval value of readOnly , which represents the interval between two screen refreshes. It is important to note that this property will not be assigned until the target 's selector is first called. the call interval for selector is calculated by calling interval time = Durationxframeinterval.


Three,GCD Way


Execute once

double delayinseconds = 2.0;

dispatch_time_t poptime = Dispatch_time (Dispatch_time_now, delayinseconds * nsec_per_sec);

Dispatch_after (Poptime, Dispatch_get_main_queue (), ^ (void) {

// execution Events

});


Repeated execution


Nstimeinterval period = 1.0; // set the time interval

dispatch_queue_t queue = Dispatch_get_global_queue (Dispatch_queue_priority_default,0);

dispatch_source_t _timer = dispatch_source_create (Dispatch_source_type_timer, 0, 0, queue);

Dispatch_source_set_timer (_timer, Dispatch_walltime (NULL,0), period * nsec_per_sec, 0); // executes per second

Dispatch_source_set_event_handler (_timer, ^{

// perform the event here

});

Dispatch_resume (_timer);


Three types of timers in IOS Nstimer, Cadisplaylink, GCD

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.