When it comes to timers, Nstimer is definitely the one we know best.
But Nstimer has a lot of shortcomings, not accurate.
Popular point is that it should do his thing, but because of other events, Nstimer will give up what he should do.
And the GCD timer is not going to happen to this kind of thing.
GCD in strict accordance with the prescribed specifications to do things.
The previous introduction of Runloop has introduced the Nstimer.
This is not the introduction.
Here is an introduction to the GCD timer.
First, we know that Nstimer is implemented on the basis of Runloop, but Runloop is based on GCD, so GCD can be considered more advanced.
Take a look at the demo effect first
Some details in the code comment
////VIEWCONTROLLER.M//CX GCD Timers////Created by Ma C on 16/3/30.//copyright©2016 year Xubaoaichiyu. All rights reserved.//#import "ViewController.h"Nsinteger Count=0;@interfaceViewcontroller ()//Note * * Not needed here?? Can be understood as dispatch_time_t already contains@property (nonatomic, strong) dispatch_source_t time;@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload];}-(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *)Event{NSLog (@"Welcome to Xu Bao love to eat Fish blog"); //Get Queuedispatch_queue_t queue = Dispatch_get_global_queue (0,0); //Create a timerSelf.time = Dispatch_source_create (Dispatch_source_type_timer,0,0, queue); //Set start timedispatch_time_t start = Dispatch_time (Dispatch_time_now, (int64_t) (3.0*nsec_per_sec)); //set the time intervaluint64_t interval = (uint64_t) (2.0*nsec_per_sec); //Set TimerDispatch_source_set_timer (Self.time, start, interval,0); //Set CallbackDispatch_source_set_event_handler (Self.time, ^{NSLog (@"Xu Bao love to eat fish"); //set when performing five times is the cancel timercount++; if(Count = =5) {dispatch_cancel (self.time); } }); //Since the timer is paused by default, let's start it up.//Start TimerDispatch_resume (self.time); }@end
In front of that, GCD is not going to be unstable so let's test it out here and we'll show you the code.
(Add a TextView)
IOS GCD Timers