The difference between Nstimer Scheduledtimerwithtimeinterval and Timerwithtimeinterval and Initwithfiredate

Source: Internet
Author: User



The English text is like this:



A Timer object can be registered on only one run loop at a time, although it can is added to multiple run loop modes Withi n that run loop. There is three ways to create a timer:


  • Use the scheduledTimerWithTimeInterval:target:selector:userInfo:repeats: or class method to create the timer and schedule it on the current run loop in the D scheduledTimerWithTimeInterval:invocation:repeats: Efault mode.
  • use the timerwithtimeinterval:invocation:repeats:  or < Code class= "Code-voice" >timerwithtimeinterval:target:selector:userinfo:repeats:  class method to create the timer object without scheduling it on a run loop. (After creating it, you must add the ' timer to a ' run loop manually by calling The addtimer:formode:  method of the Corresponding nsrunloop object.)
  • allocate the timer and initialize it using The < Code class= "Code-voice" >initwithfiredate:interval:target:selector:userinfo: Repeats:   method. (After creating it, you must add the ' timer to a ' run loop manually by calling The addtimer:formode:  method of the Corresponding nsrunloop object.)


Once scheduled on a run loop, the timer fires at the specified interval until it is invalidated. A non-repeating Timer invalidates itself immediately after it fires. However, for a repeating timer, you must invalidate the timer object yourself by calling itinvalidatemethod. Calling this method requests the removal of the timer from the current run loop; As a result, you should the method from theinvalidatesame thread on which the timer is installed. Invalidating the timer immediately disables it so that it no longer affects the run loop. The run loop then removes the timer (and the strong reference it had to the timer), either just before theinvalidatemethod re Turns or at some later point. Once invalidated, timer objects cannot be reused.






This is basically the way it is translated:



There are three ways to create a timer



1. Using scheduledtimerwithtimeinterval



Class methods create timers and progress on the current run loop in default mode (Nsdefaultrunloopmode)



2. Using timerwithtimerinterval



class method Creation Timer object does not dispatch run loop (runloop)



When you create it, you must manually add a timer to run the loop by calling the Adddtimer:formode: method corresponding to the Nsrunloop object



3. Using Initwithfiredate



In creating it, you must manually add a timer to run the loop, by dripping ah with Addtimer:formode: method corresponding to the Nsrunloop object






1.


- (void)execute {
    NSTimer *timer = [NSTimer timerWithTimeInterval:2.0 target:self selector:@selector(test) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
    [[NSRunLoop currentRunLoop] run];
}



2.


-(void) execute {
     [NSTimer scheduledTimerWithTimeInterval: 2.0 target: self selector: @selector (test) userInfo: nil repeats: YES];
     // Why is this run not needed in the main thread? That is because the main thread has RunLoop and is already started.
     [[NSRunLoop currentRunLoop] run];
}  





The two code effects are the same, scheduledtimerwithtimeinterval equivalent to Timerwithtimeinterval's two sentences.






The difference between Nstimer Scheduledtimerwithtimeinterval and Timerwithtimeinterval and Initwithfiredate


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.