IOS GCD Timers

Source: Internet
Author: User
Tags gcd

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

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.