IOS development and timer usage
In iOS development, we can call a method on a regular basis in three ways. For ease of use, I directly write code and comments in Xcode.
First, we define a method for scheduled execution.
-(Void) reloop {NSLog (@ "loop execution ");}
The three methods are discussed below.
1. Use NSTimer
// 1. Use nstimer to create A timer //. automatically add the main loop NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval: 2 target: self selector: @ selector (reloop) userInfo: nil repeats: YES]; // activate the timer [timer fire]; // You must manually release the timer during cyclic calling; otherwise, you do not need to manually release the [timer invalidate];
// B. manually add the main loop NSTimer * timer = [NSTimer timerWithTimeInterval: 2 target: self selector: @ selector (reloop) userInfo: nil repeats: YES]; [timer fire]; [[nsunloop mainRunLoop] addTimer: timer forMode: NSDefaultRunLoopMode];
Note that the thread may be blocked, so the timer created by this method will be delayed.
2. Use CADisplayLink
// Called when the screen is refreshed // CADisplayLink is a Timer class that allows us to draw specific content to the screen at a frequency synchronized with the screen update rate. After 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 content refreshed, the selector corresponding to the CADisplayLink class will be called once. Therefore, the screen refresh frequency of the iOS device is usually fixed according to the screen refresh rate of 60 times/second // delay // The screen refresh frequency of the iOS device, CADisplayLink is normally called at the end of each refresh, with a high accuracy. However, if the called method is time-consuming and exceeds the screen refresh cycle, several callback call opportunities will be skipped. // If the CPU usage is too busy and the screen refresh rate cannot be guaranteed for 60 times/Second, several chances of calling the callback method will be skipped. The number of skipping times depends on the CPU usage. // Use Case // It can be seen from the principle that CADisplayLink is suitable for non-stop repainting of the interface. For example, you need to continuously obtain the next frame during video playback for interface rendering. // 2.1 create the displaylink object CADisplayLink * displyLink = [CADisplayLink displayLinkWithTarget: self selector: @ selector (reloop)]; // 2.2 Add the object to the loop [displyLink addToRunLoop: [nsunloop currentRunLoop] forMode: NSDefaultRunLoopMode]; // 2.3 release (stop loop) if no longer needed [displyLink invalidate]; displyLink = nil;
3. Use GCD
// Run the double delayInSeconds = 2.0; interval popTime = dispatch_time (DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC); dispatch_after (popTime, interval (), ^ (void) {// execution event [self reloop];}); // repeat NSTimeInterval period = 1.0; // set the interval dispatch_queue_t queue = dispatch_get_global_queue (timeout, 0 ); dispatch_source_t _ timer = dispatch_source_create (hour, 0, 0, queue); hour (_ timer, dispatch_walltime (NULL, 0), period * NSEC_PER_SEC, 0 ); // execute dispatch_source_set_event_handler (_ timer, ^ {// here execute the event [self reloop] ;}); dispatch_resume (_ timer );