延时执行

2017-05-01  本文已影响0人  a315c2a13dc5

在程序当中经常需要延时执行某些操作,而常用的延时方法有四种。

performSelector方法

声明

- (void)performSelector:(SEL)aSelector withObject:(nullable id)anArgument afterDelay:(NSTimeInterval)delay ;

代码举例

[self performSelector:@selector(method:) withObject:nil afterDelay:2.0] ;

注意

打印结果为<NSThread: 0x60800007c440>{number = 1, name = main},方法调用成功。

    - (void)viewDidLoad {
        [super viewDidLoad] ;
        dispatch_async(dispatch_queue_create("test", NULL), ^{
            [self performSelector:@selector(method) withObject:nil afterDelay:2.0] ;
        }) ;
    }
    - (void)method {
        NSLog(@"%@",[NSThread currentThread]) ;
    }

无打印结果,方法调用失败。

    - (void)viewDidLoad {
        [super viewDidLoad] ;
        dispatch_async(dispatch_queue_create("test", NULL), ^{
            [self performSelector:@selector(method) withObject:nil afterDelay:2.0] ;
            [[NSRunLoop currentRunLoop] addPort:[NSPort port] forMode:NSDefaultRunLoopMode] ;
            [[NSRunLoop currentRunLoop] run] ;
        }) ;
    }
    - (void)method {
        NSLog(@"%@",[NSThread currentThread]) ;
    }

打印结果为<NSThread: 0x608000073e40>{number = 3, name = (null)},方法调用成功。

NSTimer定时器

声明

+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(nullable id)userInfo repeats:(BOOL)yesOrNo ;

代码举例

 NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(method:) userInfo:nil repeats:NO] ;

注意

    - (void)viewDidLoad {
        [super viewDidLoad] ;
        NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(method) userInfo:nil repeats:NO] ;
    }
    - (void)method {
        NSLog(@"%@",[NSThread currentThread]) ;
    }

打印结果为<NSThread: 0x600000261680>{number = 1, name = main},方法调用成功。

    - (void)viewDidLoad {
        [super viewDidLoad] ;
        dispatch_async(dispatch_queue_create("test", NULL), ^{
             NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(method) userInfo:nil repeats:NO] ;
        }) ;
    }
    - (void)method {
        NSLog(@"%@",[NSThread currentThread]) ;
    }

无打印结果,方法调用失败。

    - (void)viewDidLoad {
        [super viewDidLoad] ;
        dispatch_async(dispatch_queue_create("test", NULL), ^{
             NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(method) userInfo:nil repeats:NO] ;
            [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode] ;
            [[NSRunLoop currentRunLoop] run] ;
        }) ;
    }
    - (void)method {
        NSLog(@"%@",[NSThread currentThread]) ;
    }

打印结果为<NSThread: 0x6000002615c0>{number = 3, name = (null)},方法调用成功。

NSThread的sleep方法

声明

+ (void)sleepForTimeInterval:(NSTimeInterval)ti ;

代码举例

[NSThread sleepForTimeInterval:2.0] ; 
[self method] ;

注意

GCD

声明

void dispatch_after(dispatch_time_t when, dispatch_queue_t queue, dispatch_block_t block) ;

代码举例

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    [self method] ;
});

注意

上一篇 下一篇

猜你喜欢

热点阅读