Reprinted from Http://www.cocoachina.com/ios/20150519/11857.html
In the software development process, we often need to execute a method after a certain period of time, or to follow a certain cycle of execution of a method. At this time, we need to use the timer.
However, there are many ways to accomplish these tasks in iOS, how many methods do you have? After consulting the data, there are about three ways: Nstimer, Cadisplaylink, GCD. I'll explain how they're used in the next step.
First, Nstimer
1. Create a method
Nstimer *timer = [Nstimer scheduledtimerwithtimeinterval:1.0 target:self selector: @selector (action:) Userinfo:nil Repeats:no];
TimerInterval: The time to wait before executing. For example, set to 1. 0, which represents 1 seconds after the execution method target: The object that needs to execute the method. Selector: The method to be executed repeats: whether a loop is required
2. Release method
[Timer invalidate];
Attention:
After calling the Create method, the target object's counter will be incremented by 1 until the execution is complete, automatically minus 1. If it is a loop execution, it must be closed manually, otherwise the release method may not be performed.
3. Features
There is a delay
The timing of the actual trigger event for a timer, whether it be a one-time or periodic, will be related to the Runloop and Runloop mode being added, and if the runloop is performing a continuous operation, the timer will be delayed. A repetitive timer encounters this situation, and if the delay exceeds a period, it is executed immediately after the delay and continues with the period specified earlier.
Must join Runloop
Using the creation method above, the timer is automatically added to the Mainrunloop Nsdefaultrunloopmode. If you create a timer using the following method, you must manually join the Runloop:
Nstimer *timer = [Nstimer timerwithtimeinterval:5 target:self selector: @selector (timeraction) UserInfo: Nil Repeats:yes]; [[Nsrunloop Mainrunloop] Addtimer:timer Formode:nsdefaultrunloopmode];
Second, Cadisplaylink
1. Create a method
= [Cadisplaylink displaylinkwithtarget:self selector: @selector (handledisplaylink:)]; [Self.displaylink Addtorunloop:[nsrunloop Currentrunloop] formode:nsdefaultrunloopmode];
2. Stop method
[Self.displaylink invalidate]; = nil;
* * When the Cadisplaylink object is add to the Runloop, selector can be called periodically, similar to the repetition of the Nstimer is started, and when performing the invalidate operation, The Cadisplaylink object is removed from the Runloop, and the selector call is stopped, similar to the Invalidate method of the Nstimer. **
3. Features
Called when the screen refreshes
Cadisplaylink is a timer class that allows us to draw specific content to the screen at a frequency that is synchronized with the screen refresh rate. Once the Cadisplaylink is registered to Runloop in a specific mode, Runloop sends a specified selector message to the target specified by Cadisplaylink whenever the screen displays the end of the content refresh. The corresponding selector of the Cadisplaylink class is called once. So typically, follow the iOS device screen refresh rate 60 times/second
Delay
The screen refresh rate for iOS devices is fixed, and cadisplaylink is normally called at the end of each flush, with very high accuracy. However, if the calling method is more time-consuming than the screen refresh cycle, it can cause several callback invocation opportunities to be skipped.
If the CPU is too busy to guarantee a 60-time/second refresh rate on the screen, it will cause the chance to skip several calls to the callback method, depending on how busy the CPU is.
Usage Scenarios
It can be seen from the principle that Cadisplaylink is suitable for the continuous redrawing of the interface, such as video playback need to constantly get the next frame for the interface rendering.
4. Important Attributes
Frameinterval
The value of the Nsinteger type, used to set the interval for how many frames are called once selector method, the default value is 1, which is called once per frame.
Duration
The Cftimeinterval value of ReadOnly, which represents the interval between two screen refreshes. It is important to note that this property will not be assigned until the target's selector is first called. The call interval for selector is calculated by calling interval time = Durationxframeinterval.
Three, GCD way
Execute once
Double 2.0 = Dispatch_time (Dispatch_time_now, delayinseconds *^ (void) { // Execute event });
Repeated execution
Nstimeinterval period = 1.0 ; // set time interval dispatch_queue_t queue = Dispatch_get_global_queue (dispatch_queue_ Priority_default, 0 );d ispatch_source_t _timer = Dispatch_source_create (Dispatch_source_type_timer, 0 , 0 0 ), period * nsec_per_sec, 0 ); // Dispatch_source_set_event_handler per second (_timer, ^ { // Execute events here
Timers in iOS