There are several methods and usage scenarios for iOS latency execution, and ios latency

Source: Internet
Author: User

There are several methods and usage scenarios for iOS latency execution, and ios latency

I recently learned several methods for delayed execution. I will share with you the. 1. Using mselector (NSObject) method 2. nstject method 3. GCD Method 4. sleep (NSThread) method.

Delayed code execution:

- (void)delayDo : (id)sender {    NSLog(@"do:%@",sender);}

1. descrimselector (NSObject) Method

This is a common method for delayed execution in iOS.

Feature: This method must be used in the main thread. parameters can be passed. operation can be canceled and cannot be paused.

1 // without parameters 2 [self defined mselector: @ selector (delayDo :) withObject: nil afterDelay: 1.0f]; 3 // with parameters 4 [self defined mselector: @ selector (delayDo :) withObject: @ "abc" afterDelay: 1.0f];

There are two types of cancellation operations:

(1) cancel all delayed operations

[NSObject cancelPreviousPerformRequestsWithTarget:self];

(2) cancel the specified delayed operation

When you cancel a specified delayed operation, the unique identifier of the operation is the passed parameter. You can cancel the operation only when the correct parameter is passed.

1 // method 2 for canceling parameter transfer without NSObject cancelPreviousPerformRequestsWithTarget: self selector: @ selector (delayDo :) object: nil]; 3 // Method 4 for canceling parameter transfer [NSObject failed: self selector: @ selector (delayDo :) object: @ "abc"];

 

2. NSTimer Method

Timer-based latency

Features: This method must be used in the main thread. You can pass parameters. You can cancel the operation, pause the operation, and immediately perform the delay operation.

This method can be paused. Here, an isRun variable is set to determine whether it is running.

1 // timer object 2 NSTimer * timer; 3 // variable 4 BOOL isRun used as the timer to determine the status;

Start timing, repeats: NO. Only one. YES is executed, and the loop is repeated.

1  isRun = YES;2  timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(delayDo:) userInfo:@"abc" repeats:YES];

Pause:

1 if (isRun) {2 # warning this method is paused. In fact, the time is extended by 3 isRun = NO; 4 [timer setFireDate: [NSDate distantFuture]; 5} 6 else if (! IsRun) {7 # warning restoration timer 8 isRun = YES; 9 [timer setFireDate: [NSDate date]; 10}

If you do not wait for the timer, immediately perform the delay operation.

1     [timer fire];

Destroy/permanently cancel Timer

1 # waring this method cancels the policy and does not suspend Timer 2 [timer invalidate];

It should be noted that only the invalidate method can destroy the timer. When the repeats attribute is set to NO, the timer will be automatically destroyed after running.

What about parameters? The NSTimer parameter is userInfo. Therefore, you must use the userInfo method to extract the correct parameter.

1 NSlog(@"sender:%@",[sender userInfo]);

 

3. GCD Method

Features: This method does not limit threads and is not easy to cancel operations.

Why cannot I cancel the operation? The code is handed over to GCD for automatic processing, which is difficult for developers to operate.

1 // execute 2 dispatch_after (dispatch_time (DISPATCH_TIME_NOW, (int64_t) (3 * NSEC_PER_SEC), dispatch_get_main_queue (), ^ {3 [self delayDo: @ "GCD"]; 4}); 5 // execute 6 dispatch_after (dispatch_time (DISPATCH_TIME_NOW, (int64_t) (5 * NSEC_PER_SEC), wait (seconds, 0), ^ {7 [self delayDo: @ "Global-GCD"]; 8 });

Change (5 * NSEC_PER_SEC) to change the delay time in seconds.

By default, the code is executed in the main thread by changing dispatch_get_main_queue () -------> dispath_get_global_queue.

 

4. Sleep (NSThread) Method

Features: The current thread of the card master is used to implement delayed operations. Exercise caution when using this feature. In some cases, it is very convenient to use it.

1 [NSThread sleepForTimeInterval:3];

It is best not to use it in the main thread; otherwise, the interface will be stuck.

 

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.