iOS development, use of timers

Source: Internet
Author: User

In iOS development, there are three ways to implement the function of calling a method on a timed basis. For the sake of simplicity, I write code and comments directly in Xcode.

First we define a method that is executed on a timed basis.

-(void) Reloop {    NSLog (@ "loop execution");}

Let's discuss these three ways separately.

1. Using Nstimer

1. Create timer with Nstimer    //A. Auto-join main loop    nstimer *timer = [Nstimer scheduledtimerwithtimeinterval:2 target:self Selector: @selector (reloop) Userinfo:nil Repeats:yes];    Activation timer    [Timer fire];//in the loop call, you must manually release the timers, otherwise you do not have to manually release the    [timer invalidate];

B. Manually join the main loop    nstimer *timer = [Nstimer timerwithtimeinterval:2 target:self selector: @selector (reloop) Userinfo:nil Repeats:yes];    [Timer fire];    [[Nsrunloop Mainrunloop] Addtimer:timer Formode:nsdefaultrunloopmode];

Note that the timer created by this method will be delayed because the thread may be blocked.

2. Using Cadisplaylink

Call    //Cadisplaylink on screen refresh is a timer class that lets us draw specific content to the screen at a frequency that is synchronized 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 normally, the refresh rate on the iOS device screen is 60 times/sec    //delay    //iOS device screen refresh frequency is fixed, cadisplaylink under normal circumstances will be called at the end of each refresh, the accuracy is quite high. However, if the calling method is more time-consuming than the screen refresh cycle, it can cause several callback invocation opportunities to be skipped.    //If the CPU is too busy to guarantee a 60/second refresh rate on the screen, it will cause the opportunity to skip several calls to the callback method, depending on how busy the CPU is.    //Use the scene//    from the principle can be seen, cadisplaylink suitable for the interface of the continuous repainting, such as video playback need to constantly get the next frame for the interface rendering.    //2.1 Create a DisplayLink object    Cadisplaylink *displylink = [Cadisplaylink displaylinkwithtarget:self selector: @selector (Reloop)];    2.2 Add the object to the Loop    [Displylink addtorunloop:[nsrunloop Currentrunloop] formode:nsdefaultrunloopmode];            2.3 Release (stop cycle) if not needed    [Displylink invalidate];    Displylink = nil;
3. Using GCD

Executes only once    double delayinseconds = 2.0;    dispatch_time_t poptime = Dispatch_time (Dispatch_time_now, delayinseconds * nsec_per_sec);    Dispatch_after (Poptime, Dispatch_get_main_queue (), ^ (void) {        //execute event        [self reloop];    });            Repeat execution    nstimeinterval period = 1.0;//Set interval    dispatch_queue_t queue = Dispatch_get_global_queue (Dispatch_ Queue_priority_default, 0);    dispatch_source_t _timer = dispatch_source_create (dispatch_source_type_timer, 0, 0, queue);    Dispatch_source_set_timer (_timer, Dispatch_walltime (NULL, 0), period * nsec_per_sec, 0); Execute Dispatch_source_set_event_handler per second    (_timer, ^{        //execute event here        [self reloop];    });    Dispatch_resume (_timer);



iOS development, use of 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.