简体   繁体   English

串行分派队列中的块未执行-GCD iOS Objective-C

[英]Block in serial dispatch queue not executing - GCD iOS Objective-C

I've got a for loop enqueuing blocks on to the main queue as follows: 我有一个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 ...
      }
  }

Only the first block gets executed. 仅第一个块被执行。 I've verified that the loop is working properly, it is looping the proper number of times and no exceptions or error are raised. 我已经验证了循环工作正常,正在循环正确的次数,并且没有引发异常或错误。

With this code: 使用此代码:

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!!");
    });
}

"Blah" gets logged out 5 times. “ Blah”被注销5次。 I'm guessing the code you posted isn't identical to the code you have in the app? 我猜您发布的代码与应用程序中的代码不同? I had to add the ); 我必须添加); to the end of the dispatch_after call. 到dispatch_after调用的末尾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 iOS 3.x 中的 GCD 串行调度队列等价物 - Equivalent of GCD serial dispatch queue in iOS 3.x 是否复制了该对象或将其引用转移到此闭包中? (GCD队列中的Objective-C块) - Is this object copied or is its reference carried over into this closure? (Objective-C block in a GCD queue) 使用GCD创建高优先级串行调度队列 - Create a high priority serial dispatch queue with GCD 在iOS上使用Grand Central Dispatch,如果常规Objective-C块不在调度队列中,它们会运行什么队列(如果有的话)? - Using Grand Central Dispatch on iOS, what queue (if any) do regular Objective-C blocks run on if they are not in a dispatch queue? Objective-C GCD等待块完成(AFNetworking) - Objective-C GCD wait for Block to finish (AFNetworking) 在块中保留自我的危害是什么?(Objective-C,GCD) - What's the harm of retain self in block?(Objective-C, GCD) Objective-C延迟iOS 6中的方法dispatch_get_current_queue已弃用 - Objective-C delay a method in iOS 6 dispatch_get_current_queue deprecated iOS Objective-C仅在可见ViewController时执行GCD - IOS objective-C execute GCD only when ViewController is visible Objective-C,使用 UI 事件取消调度队列 - Objective-C, cancel a dispatch queue using UI event 在 Objective-C 中使用 GCD 的 dispatch_once 创建单例 - Create singleton using GCD's dispatch_once in Objective-C
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM