iOS-延迟操作方法总结

在实际应用中,有时候我们会需要延时执行某些操作,所以我们这里总结了四种延迟操作的方法,并简要分析了每种方法的异同。

NSObject的相关方法

第一种方法是使用NSObject类的performSelector:withObject:afterDelay:方法,该方法可以指定延迟一段时间后执行某个特定方法。这个方法必须在主线程中执行,否则会无效。同时该方法也是非阻塞的,无法取消延迟指向?(待考证)。

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

NSTimer

第二种方法可以使用NSTimer,该类也主要有三种方法生成一个NSTimer对象,其中我们可以更具需要选用合适的方法。通过该方法执行的延时操作需要在主线程中运行,也是非阻塞的方式。如果需要取消延迟执行操作,可以调用invalidate:操作停止一个计时器对象。

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

GCD

通过GCD的方式执行延时操作是比较灵活的,因为这种方法既可以在主线程也可以在子线程上执行,且为非阻塞的方式,不足之处是尚无取消延迟操作的方法。

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

这种方法是很简单除暴的,就是让线程休眠一段时间,然后再执行需要的操作。因此这种方法是阻塞式的,不推荐在主线程中使用,因为会造成UI卡顿。可以在子线程是使用。暂无取消方法。

[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

然后哦我们可以看到结果输出.

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.