IOS中延时执行的几种方式的比较和汇总
2016-06-01 09:02
459 查看
本文列举了四种延时执行某函数的方法及其一些区别。假如延时1秒时间执行下面的方法。
1.performSelector方法
此方式要求必须在主线程中执行,否则无效。
是一种非阻塞的执行方式,
暂时未找到取消执行的方法。
2.定时器:NSTimer
此方式要求必须在主线程中执行,否则无效。
是一种非阻塞的执行方式,
可以通过NSTimer类的- (void)invalidate;取消执行。
3. sleep方式
此方式在主线程和子线程中均可执行。
是一种阻塞的执行方式,建方放到子线程中,以免卡住界面
没有找到取消执行的方法。
4.GCD方式
此方式在可以在参数中选择执行的线程。
是一种非阻塞的执行方式,
没有找到取消执行的方法。
字数668 阅读4565 评论0 喜欢4
公用延迟执行方法
注:此方法是一种非阻塞的执行方式,未找到取消执行的方法。
程序运行结束
注:此方法是一种非阻塞的执行方式,
取消执行方法:
程序运行结束
注:此方法是一种阻塞执行方式,建议放在子线程中执行,否则会卡住界面。但有时还是需要阻塞执行,如进入欢迎界面需要沉睡3秒才进入主界面时。
没有找到取消执行方式。
程序运行结束
注:此方法可以在参数中选择执行的线程,是一种非阻塞执行方式。没有找到取消执行方式。
程序运行结束
完整代码参见:
//
// ViewController.m
// CJDelayMethod
//
// Created by 陈杰 on 8/31/15.
// Copyright (c) 2015 chenjie. All rights reserved.
//
@interface ViewController ()
@property (nonatomic, strong) NSTimer timer;
@end
@implementation ViewController
[http://weibo.com/2728581591/profile?topnav=1&wvr=6]
- (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 喜欢4
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]
相关文章推荐
- iOS仿喜马拉雅FM做的毕业设计及总结(含新手福利源码)
- iOS---多线程实现方案一 (pthread、NSThread)
- iOS页面间几种传值方式
- IOS8之前 一些过期的方法的处理
- [iOS]Core Data浅析(二)
- iOS开发 GET、POST请求方法:NSURLSession篇
- iOS开发——从一道题看Delegate
- iOS开发 GET、POST请求方法:NSURLConnection篇
- iOS快速开发框架Bee-Framework应用和解析(三) - Message, Model, Signal
- 专访iOS开发框架BeeFramework作者郭虹宇
- iOS快速开发框架--Bee Framework
- iOS开发之runloop
- iOS开源库源码解析之Mantle
- KVC KVO delegate block NSNotification 的应用场景 以及各种坑
- iOS 中KVC、KVO、NSNotification、delegate 总结及区别
- iOS RunTime机制----让catagory能够增加属性
- 漫谈iOS程序的证书和签名机制
- iOS开发-你真的会用SDWebImage?
- iOS定位和位置信息获取
- iOS颜色转图片