在以下两行的执行之间增加延迟 [英] Adding delay between execution of two following lines

查看:66
本文介绍了在以下两行的执行之间增加延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在一个(相同)函数的两行执行之间增加延迟.有什么有利的选择吗?

I need to add delay between the execution of two lines in a(same) function. Is there is any favorable options to do this?

注意:我不需要两个不同的函数来执行此操作,并且延迟一定不会影响其他函数的执行.

Note: I don't need two different functions to do this, and the delay must not affect other functions' execution.

例如:

line 1: [executing first operation];

line 2: Delay                        /* I need to introduce delay here */

line 3: [executing second operation];

任何帮助都是有意义的.预先感谢...

Any help is appreciable. Thanks in advance...

推荐答案

您可以使用gcd来执行此操作,而无需创建其他方法

You can use gcd to do this without having to create another method

double delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
  NSLog(@"Do some work");
});

您仍然应该问自己我真的需要增加延迟吗",因为它通常会使代码复杂化并导致竞争状况

You should still ask yourself "do I really need to add a delay" as it can often complicate code and cause race conditions

这篇关于在以下两行的执行之间增加延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆