Timer, timer, is used to time, you can bind it with the action to be processed, let the action be executed after a certain period, or periodically execute.
First, how the timer works
The work of the timer and the run loop are inseparable, as we normally use application kit and Uikit to create a new app that automatically launches a runloop when the main thread of the app is launched. Therefore, using a timer in the main thread does not feel the presence of Runloop. If you want to use a timer in a sub-thread, you have to understand how the timer and Runloop work together, and create a new runloop to work with the timer.
A brief summary of the working relationship between Runloop and the timer is that the timer is only responsible for timing, and the action that the timer binds is performed by Runloop. Let's talk about the relationship in detail below.
We know that when the Runloop is running, it needs to specify the operating mode (run loop mode), and Runloop's operating mode specifies the source of the event it listens to, and the object it notifies when the event occurs. As a result, you can define multiple operating modes for Runloop, which can be enabled at the Runloop runtime.
A timer can only be registered to a runloop at the same time, but can be added to multiple operating modes in this runloop. (indicating the relationship)
Initializing a timer is to bind a timer to an action to be performed, and to register a time period to tell Runloop that the action needs to be executed after the time period has elapsed, or every other time period.
In conjunction with Runloop, the timer is only responsible for timing, and Runloop is responsible for monitoring whether the timer added in the running mode it is enabled has reached the time it was registered when it was initialized, and once a timer has been found to have reached this time, The timer is bound to perform the action (this is called the Fire the timer).
It can be seen that the time period registered by the timer is not the absolute execution time of the action. Timer binding action when the specific when the execution, to see if Runloop is running, and whether to enable the timer to join the operating mode, but also to see Runloop is dealing with a lot of things, can be timely to find that the timer has been completed.
Second, the specific use of the timer 1, create a timer (1) Use the current Runloop to create a timer
Method:
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)tiinvocation:(NSInvocation *)invocationrepeats:(BOOL)yesOrNo;
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)titarget:(id)aTargetselector:(SEL)aSelectoruserInfo:(nullableid)userInforepeats:(BOOL)yesOrNo;
The parameter timeinterval is the cycle length of the timer, and the timer will clear 01 times each time timeinterval.
These two methods complete a series of actions:
- Create a timer;
- Add it to the default operating mode of the current runloop;
- Let the timer start timing.
The timer,runloop added in this way will wait for its timing to reach TimeInterval for the first time before it performs its binding action.
(2) Create a new timer, then register it in a Runloop
Method:
+ (nstimer *) Timerwithtimeinterval :(nstimeinterval) ti Invocation :(nsinvocation *) invocation Span class= "Hljs-tag" >repeats :(BOOL) Yesorno ;
+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)titarget:(id)aTargetselector:(SEL)aSelectoruserInfo:(nullableid)userInforepeats:(BOOL)yesOrNo;
These two methods can return a timer.
The timer is then registered with the Runloop object in a running mode of the Runloop object:
-(void)addTimer:(nonnull NSTimer *) forMode:(nonnull NSString *);
The timer that is added to the Runloop will immediately start timing.
(3) Initializes a timer and indicates the point in time at which the action it binds is executed
Method:
- (instanceType)initWithFireDate:(nonnull NSDate *) interval:(NSTimeInterval) target:(nonnull id) selector:(nonnull SEL) userInfo:(nullable id) repeats:(BOOL);
Here the firedate is a time point, is relative to the system time, as long as the arrival of this point of time, Runloop will go to perform the timer binding action. So if the timer is disposable and does not need to be timed, the interval parameter will not work. If it is a repeating timer, only need to time, parameter interval only use.
You can use the Setfiredate method to set the execution time of the action to the timer.
2. Stop Timer
The timer needs to be in the management of the runloop to be effective, stop the timer, as long as it is in the Runloop from its location to clean up can be achieved. (or let Runloop stop running).
(1) for a disposable timer
The one-time timer does not have to consider this problem, because Runloop executes the timer binding action will automatically take the timer from its running mode to clean out.
(2) for periodic timer
Method:
-(void)invalidate;
This is a problem that needs to be considered for periodic timer.
To stop a periodic timer, to get a reference to the Timer object, call the Timer object's Invalidate method when it needs to clean the timer.
Of course, you can also perform this method on a one-time timer, and call the Invalidate method before the timer action is Runloop executed, and the timer's action will never be executed.
IOS Timer Timer