Use of timers in iOS

Source: Internet
Author: User

1. Nstimer is not very precise.

2.CADisplayLink screen

3. GCD to achieve a fixed time device

Timed Loop Execution Events

The Dispatch_source_set_timer method is worth mentioning is the last parameter (leeway), he tells the system we need timer trigger accuracy degree.

All timers are not guaranteed to be 100% accurate, this parameter is used to tell the system you want the system to ensure the accuracy of the effort level.

If you want a timer to be triggered every 5 seconds, and the more accurate the better, then you pass 0 as the parameter.

In addition, if it is a recurring task, such as checking email, then you will want to check every 10 minutes, but not so accurate.

So you can pass in 60 and tell the system that 60 seconds of error is acceptable. His purpose is to reduce resource consumption.

Inside the global queue

_timer = dispatch_source_create (dispatch_source_type_timer, 0, 0, globalqueue);

Set how often to execute

Dispatch_source_set_timer (_timer, Dispatch_time_now, 1.0 * nsec_per_sec, 0.0 * nsec_per_sec);

Execution events

Dispatch_source_set_event_handler (_timer, ^{//Timer event handler

DLog (@ "Event Handler");

if (Timeout <= 0) {

Dispatch_source_cancel (_timer);//Cancel timed loop timer; make handle called, event is executed

Dispatch_async (Mainqueue, ^{

if (![ Cmadmanager shareinstance].launchadclicked) {

if (!self.skipbutton.selected) {

[Self SkipAction:self.skipButton];

}

}

});

} else {

NSString *strtime = [NSString stringwithformat:@ "%d s Skip", timeout];

Executes in the main queue

Dispatch_async (Mainqueue, ^{

[Self.skipbutton settitle:strtime Forstate:uicontrolstatenormal];

});

timeout--;

}

});

Handle

Dispatch_source_set_cancel_handler (_timer, ^{

Timer cancels the processor; execute when calling Dispatch_source_cancel

DLog (@ "Cancel Handler");

});

//

Dispatch_resume (_timer);

Resume timed cycle timer; The default state is suspended after the Dispatch Source is created and requires active recovery, otherwise the event will not be passed and will not be executed

Contrast

CADisplayLinkAnd NSTimerWhat's the difference?

The screen refresh frequency of the IOS device is fixed and is CADisplayLink normally called at the end of each flush, with very high accuracy.
NSTimerThe accuracy is low, such as when the NSTimer trigger time, runloop if in the blocking state, the trigger time will be postponed to the next runloop cycle. And NSTimer added tolerance properties that allow the user to set the delay range for the time that can be tolerated.
CADisplayLinkThe use of a relatively exclusive, suitable for the UI of the constant repainting, such as custom animation engine or video playback of the rendering. NSTimeruse 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 CADisplayLink NSTimer 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.

Use of timers in iOS

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.