且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

dispatch_after循环/重复

更新时间:2023-02-09 18:05:31

无论计划何时运行dispatch_after(...)调用,它都会立即返回.这意味着您的循环在分派它们之间不会等待两秒钟.相反,您正在建立一个无限的事物队列,该事物将从现在开始两秒钟发生,彼此之间不会两秒钟.

The dispatch_after(...) call returns immediately no matter when it is scheduled to run. This means that your loop is not waiting two seconds between dispatching them. Instead you are building an infinite queue of things that will happen two seconds from now, not two seconds between each other.

所以是的,您陷入了添加越来越多要执行的块的无限循环中.如果您希望每两秒钟发生某件事,那么您可以使用重复的NSTimer或在其内部包含块dispatch_after(这样,第二个块将在第一个块之后运行两秒钟).

So yes, you are stuck in an infinite loop of adding more and more blocks to be executed. If you want something to happen every two second then you could use a repeating NSTimer or have the block dispatch_after inside itself (so that the second block runs two seconds after the first).