简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM