IOS中延时执行的几种方式的比较和汇总

本文列举了四种延时执行某函数的方法及其一些区别。假如延时1秒时间执行下面的方法。

- (void)delayMethod { NSLog(@"execute"); }

1.performSelector方法

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

此方式要求必须在主线程中执行,否则无效。
是一种非阻塞的执行方式,
暂时未找到取消执行的方法。

2.定时器:NSTimer

[NSTimer scheduledTimerWithTimeIn<wbr>terval:1.0f target:self selector:@selector(delayMethod) userInfo:nil repeats:NO];</wbr>

此方式要求必须在主线程中执行,否则无效。
是一种非阻塞的执行方式,
可以通过NSTimer类的- (void)invalidate;取消执行。

3. sleep方式

[NSThread sleepForTimeInterval:1.0f]; [self delayMethod];

此方式在主线程和子线程中均可执行。
是一种阻塞的执行方式,
建方放到子线程中,以免卡住界面
没有找到取消执行的方法。

4.GCD方式

double delayInSeconds = 1.0; <wbr></wbr>
 <wbr>__block ViewController* bself = self; <wbr></wbr></wbr>
 <wbr>dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));</wbr>
<span style="margin: 0px; padding: 0px; word-wrap: normal; word-break: normal;">dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ <wbr></wbr></span>
 <wbr>[bself delayMethod]; });</wbr>

此方式在可以在参数中选择执行的线程。
是一种非阻塞的执行方式,
没有找到取消执行的方法。

浅谈iOS开发中方法延迟执行的几种方式

字数668  阅读4565  评论0 

Method1. performSelector方法

Method2. NSTimer定时器

Method3. NSThread线程的sleep

Method4. GCD


公用延迟执行方法

- (void)delayMethod{ NSLog(@"delayMethodEnd");}


Method1:performSelector

[self performSelector:@selector(delayMethod) withObject:nil/*可传任意类型参数*/ afterDelay:2.0];
注:此方法是一种非阻塞的执行方式,未找到取消执行的方法。

程序运行结束
2015-08-31 10:56:59.361 CJDelayMethod[1080:39604] delayMethodStart2015-08-31 10:56:59.363 CJDelayMethod[1080:39604] nextMethod2015-08-31 10:57:01.364 CJDelayMethod[1080:39604] delayMethodEnd

Method2:NSTimer定时器

NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(delayMethod) userInfo:nil repeats:NO];
注:此方法是一种非阻塞的执行方式,
取消执行方法:- (void)invalidate;即可

程序运行结束
2015-08-31 10:58:10.182 CJDelayMethod[1129:41106] delayMethodStart2015-08-31 10:58:10.183 CJDelayMethod[1129:41106] nextMethod2015-08-31 10:58:12.185 CJDelayMethod[1129:41106] delayMethodEnd

Method3:NSThread线程的sleep

[NSThread sleepForTimeInterval:2.0];
注:此方法是一种阻塞执行方式,建议放在子线程中执行,否则会卡住界面。但有时还是需要阻塞执行,如进入欢迎界面需要沉睡3秒才进入主界面时。
没有找到取消执行方式。

程序运行结束
2015-08-31 10:58:41.501 CJDelayMethod[1153:41698] delayMethodStart2015-08-31 10:58:43.507 CJDelayMethod[1153:41698] nextMethod

Method4:GCD

__block ViewController/*主控制器*/ *weakSelf = self;

dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0/*延迟执行时间*/ * NSEC_PER_SEC));

dispatch_after(delayTime, dispatch_get_main_queue(), ^{
    [weakSelf delayMethod];
});`

注:此方法可以在参数中选择执行的线程,是一种非阻塞执行方式。没有找到取消执行方式。

程序运行结束
2015-08-31 10:59:21.652 CJDelayMethod[1181:42438] delayMethodStart2015-08-31 10:59:21.653 CJDelayMethod[1181:42438] nextMethod2015-08-31 10:59:23.653 CJDelayMethod[1181:42438] delayMethodEnd

完整代码参见:

//
// ViewController.m
// CJDelayMethod
//
// Created by 陈杰 on 8/31/15.
// Copyright (c) 2015 chenjie. All rights reserved.
//

import "ViewController.h"

@interface ViewController ()
@property (nonatomic, strong) NSTimer timer;
@end
@implementation ViewController
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. NSLog(@"delayMethodStart"); [self methodOnePerformSelector];// [self methodTwoNSTimer];// [self methodThreeSleep];// [self methodFourGCD]; NSLog(@"nextMethod");}
- (void)methodFiveAnimation{ [UIView animateWithDuration:0 delay:2.0 options:UIViewAnimationOptionAllowUserInteraction animations:^{ } completion:^(BOOL finished) { [self delayMethod]; }];}
- (void)methodFourGCD{ __block ViewController 
weakSelf = self; dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)); dispatch_after(delayTime, dispatch_get_main_queue(), ^{ [weakSelf delayMethod]; });}
- (void)methodThreeSleep{ [NSThread sleepForTimeInterval:2.0];}
- (void)methodTwoNSTimer{ NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(delayMethod) userInfo:nil repeats:NO];}
- (void)methodOnePerformSelector{ [self performSelector:@selector(delayMethod) withObject:nil/*可传任意类型参数*/ afterDelay:2.0];}
- (void)delayMethod{ NSLog(@"delayMethodEnd");}
- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end


欢迎关注笨笨编程官方微博账号

[http://weibo.com/2728581591/profile?topnav=1&wvr=6]











  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值