博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
常用的延时执行
阅读量:4330 次
发布时间:2019-06-06

本文共 948 字,大约阅读时间需要 3 分钟。

1、 performSelector 一旦定制好延时任务,不会卡住当前线程

//2秒后再调用self的run方法[self performSelector:@selector(run) withObject:nil afterDelay:2.0];

2、 使用GCD函数

// 该方法中, 会根据传入的队列来决定回掉block在哪个线程中执行  // 如果传入的是主队列, 那么block会在主线程调用  // 如果传入的是全局队列, 那么block会在子线程中调用  dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_global_queue(0, 0), ^{ // 这里传入全局队列会在子线程中执行block,如果传入主队列就会在主线程中执行block      NSLog(@"3秒之后执行  %@", [NSThread currentThread]);  });

3、 使用NSTimer

[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(test) userInfo:nil repeats:NO];- (void)test{    NSLog(@"---- begin-----");    [NSThread sleepForTimeInterval:3]; // 它会卡住线程    NSLog(@"---- end-----");}

4、 [NSThread sleepForTimeInterval:3]  会卡住线程的,不用

- (void)dely1{  // 1、延时执行不要用 sleepForTimeInterval(它会卡住线程)  NSLog(@"---- begin-----");  [NSThread sleepForTimeInterval:3];  NSLog(@"---- end-----");}

 

转载于:https://www.cnblogs.com/XXxiaotaiyang/p/5107575.html

你可能感兴趣的文章
JAVA 基础 / 第八课:面向对象 / JAVA类的方法与实例方法
查看>>
Ecust OJ
查看>>
P3384 【模板】树链剖分
查看>>
Thrift源码分析(二)-- 协议和编解码
查看>>
考勤系统之计算工作小时数
查看>>
4.1 分解条件式
查看>>
Equivalent Strings
查看>>
flume handler
查看>>
收藏其他博客园主写的代码,学习加自用。先表示感谢!!!
查看>>
H5 表单标签
查看>>
su 与 su - 区别
查看>>
C语言编程-9_4 字符统计
查看>>
在webconfig中写好连接后,在程序中如何调用?
查看>>
限制用户不能删除SharePoint列表中的条目(项目)
查看>>
【Linux网络编程】使用GDB调试程序
查看>>
feign调用spring clound eureka 注册中心服务
查看>>
ZT:Linux上安装JDK,最准确
查看>>
LimeJS指南3
查看>>
关于C++ const成员的一些细节
查看>>
《代码大全》学习摘要(五)软件构建中的设计(下)
查看>>