Summary of "turn" ios-delay operation method

Source: Internet
Author: User
Tags gcd

Original URL: http://lysongzi.com/2016/01/30/iOS-%E5%BB%B6%E8%BF%9F%E6%93%8D%E4%BD%9C%E6%96%B9%E6%B3%95%E6%80%BB%E7%BB%93/

In practical applications, sometimes we need to delay some operations, so we summarize four methods of delaying operation, and briefly analyze the similarities and differences of each method.

Related Methods of NSObject

The first method is to use the method of the NSObject class performSelector:withObject:afterDelay: , which specifies that a certain method is executed after a delay of some time. This method must be executed in the main thread, otherwise it will not be valid. At the same time, the method is non-blocking, can not cancel the delay point? (To be verified).

[self performSelector:@selector(delayMethod) withObject:nil afterDelay:1.0];
Nstimer

The second method can use Nstimer, which also has three main methods to generate a Nstimer object, in which we can more need to choose the appropriate method. The deferred operation performed by this method needs to be run in the main thread, which is also a non-blocking way. If you need to cancel the deferred action, you can call the invalidate: action to stop a timer object.

[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(delayMethod) userInfo:nil repeats:NO];
GCD

It is more flexible to perform the delay operation by GCD, because this method can be executed on both the main thread and the child thread, and in a non-blocking way, the disadvantage is that there is no way to cancel the deferred operation.

double delayInSeconds = 3.0;dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));dispatch_after(popTime, dispatch_get_main_queue(), ^{    [self delayMethod];});
Nsthread

This method is very simple to remove the storm, is to let the thread sleep for a period of time, and then perform the required operation. This method is therefore blocking and is not recommended for use in the main thread, as it will cause the UI to be stuck. Can be used in a child thread. No cancellation method is pending.

[NSThread sleepForTimeInterval:1.0];[self delayMethod];
Demon
#import "ViewController.h"    @interface ViewController ()@end@implementation ViewController- (void)viewDidLoad{    [super viewDidLoad];    //第一种方法    //在主线程执行,否则无效;非阻塞;无法取消    [self performSelector:@selector(delayMethod) withObject:nil afterDelay:1.0];    //第二种方法    //主线程中执行,否则无效;非阻塞;可以调用invalidate方法取消    [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(delayMethod) userInfo:nil repeats:NO];    //第三种方法    //可选择执行的线程;非阻塞;无法取消    double delayInSeconds = 3.0;    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));    dispatch_after(popTime, dispatch_get_main_queue(), ^{        [self delayMethod];    });    //第四种方法    //主线程子线程都可以;阻塞;无法取消    [NSThread sleepForTimeInterval:1.0];    [self delayMethod];}- (void)delayMethod{    NSLog(@"Excute.");}@end

And then oh we can see the result output.

2016-01-30 20:54:27.822 DelayPerformSelector[2079:319217] Excute.2016-01-30 20:54:27.830 DelayPerformSelector[2079:319217] Excute.2016-01-30 20:54:28.820 DelayPerformSelector[2079:319217] Excute.2016-01-30 20:54:29.818 DelayPerformSelector[2079:319217] Excute.

Summary of "turn" ios-delay operation method

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.