Nstimer in iOS and timer in Android

Source: Internet
Author: User

First Look at iOS,

Scheduling timers in Run Loops

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:invocation:repeats: or scheduledTimerWithTimeInterval:target:selector:userInfo:repeats: class method to create the timer and schedule it on the current run loop in the default mode.

  • Use the timerWithTimeInterval:invocation:repeats: or timerWithTimeInterval:target:selector:userInfo:repeats: class method to create the timer object without scheduling it on a run loop. (aftercreating it, you must add the "timer to a" run loop manually by calling the method of the addTimer:forMode: corresponding object.)

  • Allocate the timer and initialize it using the initWithFireDate:interval:target:selector:userInfo:repeats: method. (aftercreating it, you must add the "timer to a" run loop manually by calling the method of the addTimer:forMode: corresponding 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 it invalidate method. Calling this method requests the removal of the timer from the current run loop; As a result, you should the method from the invalidate same 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 the invalidate method re Turns or at some later point. Once invalidated, timer objects cannot be reused.

Summarize the following, in iOS, a timer is closely related to a runloop, with a timer, you must set its runloop, otherwise the timer will not work properly. After another timer invalid, you cannot enable it again, and you must create a new timer.

In iOS, without special settings, when an application enters the background, an application-related thread pauses immediately, and a timer that naturally executes in thread will stop running. When the application enters the foreground again, the suspended thread is restored.

Also need to note, look at the code

Dispatch_async (Dispatch_get_global_queue (dispatch_queue_priority_default, 0), ^{        NSLog (@"async ....") );         *runloop = [Nsrunloop currentrunloop];         = [Nstimer timerwithtimeinterval:1.0 target:self selector: @selector (test) Userinfo:nil Repeats:yes];        [Runloop Addtimer:timer formode:nsrunloopcommonmodes];        [Runloop run];    });

New thread if you want to start runloop, you cannot simply write [Runloop run], you must add a trigger source, such as the timer here, or runloop can not be started.

In Android,

The Timer automatically opens a new thread when it is used, such as the following code

protected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);     Setcontentview (r.layout.activity_log_in); System.out.println ("Thread.CurrentThread () ..." +Thread.CurrentThread (). GetId ()); TimerTask Task=NewTimerTask () { Public voidrun () {//put the code in here every time you need to execute it. System.out.println ("Thread.CurrentThread ()" +Thread.CurrentThread (). GetId ()); System.out.println ("......... Timertask.........run ");        }        }; Timer Timer=NewTimer (); Timer.schedule (Task,1000,1000);}

The following is the output

Thread.CurrentThread () ..... 1thread.currentthread ()319thread.currentthread ()319

From here we can see that the timer in Android is automatically created new thread and run, the main thread block will not affect the operation of the timer.

When the program enters the background, all threads of the program do not stop until the thread is stopped by the system or code.

Nstimer in iOS and timer in Android

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.