A preliminary study on iOS Multi-Threading (iv)--Nstimer Original Address http://www.cnblogs.com/sunfrog/p/3243230.html
Understanding the Run loop makes it possible to thoroughly understand the nstimer implementation principle, meaning that Nstimer actually relies on the run loop.
Let's take a look at Nstimer's two common methods:
+ (nstimer *) Timerwithtimeinterval: (nstimeinterval) ti target: (ID) atarget selector: (SEL) Aselector UserInfo: (ID) userInfo repeats: (BOOL) Yesorno; Generates a timer but does not execute
+ (nstimer *) Scheduledtimerwithtimeinterval: (nstimeinterval) ti target: (ID) atarget selector: ( SEL) Aselector UserInfo: (ID) userInfo repeats: (BOOL) Yesorno; Generates a timer and incorporates the current thread's run loop to execute
Nsrunloop and timer related methods are:
-(void) AddTimer: (nstimer *) Timer Formode: (NSString *) mode; Registering a timer on the run loop
The main thread already has run loop, so Nstimer generally runs on the main thread without having to call AddTimer:. However, running on a non-main thread must configure the run loop, which has the following sample code for the Main method:
-(void) Main
{
Nstimer *mytimer = [Nstimer scheduledtimerwithtimeinterval: 1.0 target:self selector:@selector ( Timer:) userInfo:nil repeats:YES];
Nsrunloop *runloop = [nsrunloop currentrunloop];
[Runloop Addtimer:mytimer Formode:nsdefaultrunloopmode]; In fact this step is not required,Scheduledtimerwithtimeinterval has been included in the current thread run. If you use Timerwithtimeinterval, you need
while (condition)
[Runloop Run];
}
In fact, this thread cannot exit because there is a timer event that needs to be processed and [Runloop run] will never return. The solution is to set a cutoff time:
[Runloop rununtildate:[nsdate datewithtimeintervalsincenow:10.0]; Every 10 seconds to check the thread cycle conditions, of course, the time value can be determined according to the actual situation.
We usually use Nstimer in the main thread, and there is a real problem to be aware of. When the interface is sliding, the main thread runloop will temporarily stop processing some other events for better handling of UI events and scrolling, and the Nstimer running in the main thread will be paused. The solution is to change the mode of the Nstimer operation (mode can be considered as the event type), instead of using the default Nsdefaultrunloopmode, rather than the nsrunloopcommonmodes, so the main thread will continue to process the Nstimer event. The specific code is as follows:
Nstimer *timer = [nstimer timerwithtimeinterval:1.0 target: Selfselector:@selector (timer:) userInfo:nil repeats:YES];
[[nsrunloop currentrunloop] addtimer: timer formode:nsrunloopcommonmodes];
You can see the blog post http://bluevt.org/?p=209, deepen understanding of nstimer and nsrunloop relationship.
The method of delaying calls in the previous blog post is actually registering a timer on the current thread's run loop to implement timed runs. So if you are using it on a non-main thread, you must have a run loop.
-(void) Performselector: (SEL) aselector withobject: (ID) anargument afterdelay: (nstimeinterval) delay Inmodes: (Nsarray *) modes;
-(void) Performselector: (SEL) aselector withobject: (ID) anargument afterdelay: (nstimeinterval) Delay;
A preliminary study on iOS multi-Threading (iv)--Nstimer