dispatch_after looped / repeated

前端 未结 4 1894
北海茫月
北海茫月 2021-01-06 03:36

I am trying to create a loop like this:

while (TRUE){
  dispatch_after(...{
    
  });
}

After a viewDidLoad. The idea i

4条回答
  •  清酒与你
    2021-01-06 04:22

    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.

    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).

提交回复
热议问题