在串行调度队列中阻塞不执行 - GCD iOS Objective-C

Kyl*_*arn 2 queue objective-c grand-central-dispatch ios

我有一个for循环将块排入主队列,如下所示:

  for (int x=0; x<5; x++) {
    double delayInSeconds = x * .03;
      dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
      dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        ... code here ...
      }
  }
Run Code Online (Sandbox Code Playgroud)

只有第一个块被执行.我已经验证了循环正常工作,它循环了正确的次数并且没有引发异常或错误.

Chr*_*ian 6

使用此代码:

for (int x=0; x<5; x++) {
    double delayInSeconds = x * .03;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        NSLog(@"Blah!!");
    });
}
Run Code Online (Sandbox Code Playgroud)

"Blah"被登出5次.我猜你发布的代码与你在应用程序中的代码不一样?我不得不加上); 到dispatch_after调用结束.