Three big timers in iOS

Source: Internet
Author: User
Tags gcd

Timers are often used in iOS development, and there are three types of timers commonly used in iOS, Nstime,cadisplaylink and GCD, respectively.

Nstimer Mode 1
    // 创建定时器    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(test) userInfo:nil repeats:YES]; // 停止定时器 [timer invalidate];
Mode 2
    // 创建定时器    NSTimer *timer = [NSTimer timerWithTimeInterval:2 target:self selector:@selector(test) userInfo:nil repeats:YES]; // 将定时器添加到runloop中,否则定时器不会启动 [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes]; // 停止定时器 [timer invalidate];

Mode 1 automatically adds the created timer to the current thread runloop by default, without adding it manually. However, in this mode, when scrolling the screen runloop will enter another mode, the timer will be paused, in order to solve this problem, you can add the timer as in mode 2 to Nsrunloopcommonmodes mode.

Mode 1 and Mode 2 will be set after the interval set at the time (set to 2s in this example) after the test method, if necessary to execute immediately can use the following code.

[time fire];
Cadisplaylink
    // 创建displayLink    CADisplayLink *displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(test:)];    // 将创建的displaylink添加到runloop中,否则定时器不会执行    [displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];    // 停止定时器 [displayLink invalidate]; displayLink = nil;

When the Cadisplaylink object is add to the Runloop, selector can be called periodically, similar to duplicate Nstimer is initiated, invalidate objects are removed from Cadisplaylink when the runloop operation is performed , the selector call also stops, similar to the Nstimer invalidate method

Call timing

Cadisplaylink is a timer class that synchronizes with the screen refresh rate. Once the Cadisplaylink is registered to Runloop in a specific mode, Runloop sends a specified selector message to the target specified by Cadisplaylink whenever the screen displays the end of the content refresh. The corresponding selector of the Cadisplaylink class is called once, so you can use Cadisplaylink to do something related to the screen operation.

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.

GCD Timer One-time timing
1.0 * NSEC_PER_SEC); dispatch_after(timer, dispatch_get_main_queue(), ^(void){        NSLog(@"GCD-----%@",[NSThread currentThread]); });
Recurring timer
@property (Nonatomic,Strong) dispatch_source_t timer;Note: Strong references should be used here strong{0. Create a queuedispatch_queue_t queue = Dispatch_get_main_queue ();1. Create a timer in the GCD/* First parameter: Create the type of SOURCE Dispatch_source_type_timer: Timer second parameter: 0 Third parameter: 0 Fourth parameter: Queue */dispatch_source_t timer = dispatch_source_create (Dispatch_source_type_timer,0, 0, queue); //2. Set time, etc./ /First parameter: Timer object The second parameter: Dispatch_time_now represents the third parameter from now on: Interval time gcd the smallest unit of time in nanoseconds is the fourth parameter: accuracy (indicating allowable error, 0 means absolute precision) */Dispatch_source_set_timer (timer, Dispatch_time_now, 1.0 * nsec_per_sec, 0 * nsec_per_sec); //3. The task to invoke Dispatch_source_set_event_handler (timer, ^{ NSLog (@ "GCD-----%@", [nsthread CurrentThread ]); }); //4. Start execution of Dispatch_resume (timer); // self. Timer = timer;}             

Note here 要强引用定时器 , otherwise the timer will be released after execution to}, no timing effect.

GCD timer time is very accurate, the minimum timing time can reach 1 nanoseconds, so used in very accurate timing occasions.

Three big timers in iOS

Related Article

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.