Nstimer usage Precautions in IOS

Source: Internet
Author: User

1. Initialization

+ (Nstimer *) Timerwithtimeinterval: (nstimeinterval) TI target: (ID) atarget selector: (SEL) Aselector userInfo: (ID) UserInfo repeats: (BOOL) Yesorno;

+ (Nstimer *) Scheduledtimerwithtimeinterval: (nstimeinterval) TI target: (ID) atarget selector: (SEL) Aselector UserInfo :(ID) UserInfo repeats: (BOOL) Yesorno;

Note: UserInfo is the user information that the value Nstimer carries.

without scheduled initialization, manual addtimer:formode is required: Add the timer to a runloop.

The scheduled initialization method will be added directly to the current runloop with the default mode.

Sample

[Nstimer scheduledtimerwithtimeinterval:2 Target:selfselector: @selector (startfindapartment:) UserInfo:nil repeats: YES];

Or

Nstimer *mytimer = [Nstimertimerwithtimeinterval: 3.0 target: Selfselector: @ Selector (timerfired:) userInfo: nilrepeats: NO];

[[Nsrunloopcurrentrunloop] addtimer: MyTimerformode: Nsdefaultrunloopmode];

2. Trigger (START)

When the timer is created (not scheduled, added to Runloop, the timer is automatically triggered after the timeinterval seconds specified at initialization.

Nstimer *timer=[nstimer timerwithtimeinterval:0.5target: Self selector:@selector(timeschedule)userInfo:Nil repeats:YES];

nsrunloop *runloop=[nsrunloopcurrentrunloop];

[Runloop addtimer: Timerformode:nsdefaultrunloopmode];

[Timer fire];

You can use the-(void) Fire method to trigger the timer immediately;

3. Stop

-(void) invalidate;

This is the only way to move the timer out of the Runloop.

4, in multi-threaded development, if the use of timers in the Mainthread, the two initialization methods can be used, if the use of timers in the sub-thread, can only use the method:

+ (Nstimer *) Timerwithtimeinterval: (nstimeinterval) TI target: (ID) atarget selector: (SEL) Aselector userInfo: (ID) UserInfo repeats: (BOOL) Yesorno;


and start the timer can not use fire, only let Runloop continue to execute, sample code:

_timer =[nstimertimerwithtimeinterval:0.5target: Self selector:@selector(timeschedule)userInfo:Nil repeats:YES];

nsrunloop *runloop=[nsrunloopcurrentrunloop];

[Runloop addtimer:_timerformode:nsdefaultrunloopmode];

while ([Runlooprunmode:nsdefaultrunloopmode beforedate: [ nsdate distantfuture]]);



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.