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
CADisplayLink
And
NSTimer
What'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.
NSTimer
The 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.
CADisplayLink
The use of a relatively exclusive, suitable for the UI of the constant repainting, such as custom animation engine or video playback of the rendering. NSTimer
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 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