On iOS, how does one trigger a scheduled task or a delayed task ?, Ios triggers delayed tasks

Source: Internet
Author: User

On iOS, how does one trigger a scheduled task or a delayed task ?, Ios triggers delayed tasks
In iOS, how does one trigger a scheduled task or a delayed task?

Scheduled tasks refer to periodic calls of a method to implement repeated execution of tasks, such as the countdown function. delayed tasks refer to waiting for a certain period of time before executing a task, for example, page latency jump. There are many methods to control the task latency or scheduled execution in iOS. You should pay attention to synchronization or Asynchronization, and whether the main thread will be blocked. The implementation method is as follows:

1. performSelector implements delayed tasks

Delayed tasks can be implemented by using the javasmselector of the current UIViewController to implicitly create sub-threads without blocking the main thread:

/* Task execution delayed for 10 S */[self defined mselector: @ selector (task) withObject: nil afterDelay: 10];-(void) task {// delay task}
2. Use sleep to wait for subsequent tasks. Use it with caution and block the main thread.
NSThread sleepForTimeInterval:10.0];
3. Implement delayed or scheduled tasks using GCD

Implement delayed execution of block code blocks through GCD:

dispatch_time_t delay = dispatch_time(DISPATCH_TIME_NOW, 10 * NSEC_PER_SEC);dispatch_after(delay, dispatch_get_main_queue(), ^{      // delay task});

GCD can also be used to define the timer to implement the timer function. You can set the delay to enable the timer. Note that you must define a strong pointer to the timer object to make the timer take effect:

/* The timer must be directed by a strong pointer to take effect */@ property (nonatomic, strong) dispatch_source_t timer;/* define the timer on the specified Thread */dispatch_queue_t queue = dispatch_get_global_queue, 0); timer _ timer = dispatch_source_create (timer, 0, 0, queue);/* Start time */dispatch_time_t when = dispatch_time (DISPATCH_TIME_NOW, (int64_t) (1.0 * NSEC_PER_SEC);/* set the timer */dispatch_source_set_ti Mer (_ timer, when, 1.0 * NSEC_PER_SEC, 0);/* timer callback block */dispatch_source_set_event_handler (_ timer, ^ {NSLog (@ "dispatch_source_set_timer is working! ") ;});/* Enable timer */dispatch_resume (_ timer);/* strongly reference timer object */self. timer = _ timer;
4. NSTimer implement scheduled tasks 5. CADisplayLink implement scheduled tasks

The timer implemented by CADisplayLink is bound with the screen refresh frequency. It is a frame rate refresh and is suitable for the constant re-painting of the interface (such as smooth animation and video playback ). After CADisplayLink is registered to runloop in a specific mode, a message is sent to the target specified by CADisplayLink every time the content is refreshed on the screen. You can also set the call time for each frame as needed. By default, each frame is called. In addition, CADisplayLink can also obtain information such as frame rate and time.

The calling of each frame of the CADisplayLink implementation method results in a very high timing accuracy. However, if the calling method is time-consuming and exceeds the interval of one frame, the frame will jump, the number of frame hops depends on the CPU usage.

-(Void) viewDidLoad {[super viewDidLoad]; CADisplayLink * displayLink = [CADisplayLink displayLinkWithTarget: self selector: @ selector (displayLink_SEL)]; /* add it to the currently running RunLoop to start */[displayLink addToRunLoop: [nsunloop currentRunLoop] forMode: NSDefaultRunLoopMode]; /* pause and continue the selector call * // [displayLink setPaused: YES]; // [displayLink setPaused: NO];/* set the number of frames to call the selector once, the default value is 1 * // [displayLink setPreferre DFramesPerSecond: 2];/* remove, no longer use * // [displayLink invalidate]; // displayLink = nil;}-(void) displayLink_SEL {NSLog (@ "displayLink is working! ");}

The output is as follows. selector is called for each frame:

2018-02-01 11:48:02.707283+0800 IOSDemo[42328:4522489] displayLink is working!2018-02-01 11:48:02.724375+0800 IOSDemo[42328:4522489] displayLink is working!2018-02-01 11:48:02.742995+0800 IOSDemo[42328:4522489] displayLink is working!

Related Article

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.