美文网首页
GCD for 循环延迟执行

GCD for 循环延迟执行

作者: 奕珃 | 来源:发表于2023-07-06 15:29 被阅读0次

    0.05 为延迟时间,可调

    dispatch_queue_t serialQueue = dispatch_queue_create("serialQueue", DISPATCH_QUEUE_SERIAL);

    dispatch_async(serialQueue, ^{

                dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);

                 for (i=0 ; i<100; i++) {

                         dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.05 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

                                    //在这里执行相关代码

                                    NSLog(@"当前i  ==== %d",i);                        

                                  dispatch_semaphore_signal(semaphore);

                         });

                    dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);

            }

    });

    相关文章

      网友评论

          本文标题:GCD for 循环延迟执行

          本文链接:https://www.haomeiwen.com/subject/vtgludtx.html