dispatch_after looped / repeated

前端 未结 4 1893
北海茫月
北海茫月 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:02

    Yes, you can do that with gcd. You need two additional c-functions though.

    static void dispatch_async_repeated_internal(dispatch_time_t firstPopTime, double intervalInSeconds, dispatch_queue_t queue, void(^work)(BOOL *stop)) {    
        __block BOOL shouldStop = NO;
        dispatch_time_t nextPopTime = dispatch_time(firstPopTime, (int64_t)(intervalInSeconds * NSEC_PER_SEC));
        dispatch_after(nextPopTime, queue, ^{
            work(&shouldStop);
            if(!shouldStop) {
                dispatch_async_repeated_internal(nextPopTime, intervalInSeconds, queue, work);
            }
        });
    }
    
    void dispatch_async_repeated(double intervalInSeconds, dispatch_queue_t queue, void(^work)(BOOL *stop)) {
        dispatch_time_t firstPopTime = dispatch_time(DISPATCH_TIME_NOW, intervalInSeconds * NSEC_PER_SEC);
        dispatch_async_repeated_internal(firstPopTime, intervalInSeconds, queue, work);
    }
    

    Tested! Works as intended.

    https://gist.github.com/4676773

    0 讨论(0)
  • 2021-01-06 04:10

    If you'd like an async task to run after a delay to check for example if a tag has been updated, then finish, you could use the code below:

    typedef void (^RepeatCompletionHandler)(BOOL isRepeat);
    
    typedef void (^RepeatBlock)(RepeatCompletionHandler completionHandler);
    
    - (void)dispatchRepeat:(int)seconds withBlock:(RepeatBlock)block {
    
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, seconds * NSEC_PER_SEC),
                       dispatch_get_main_queue(), ^() {
                           block(^(BOOL isRepeat) {
                               if (isRepeat) {
                                   return [self dispatchRepeat:seconds withBlock:block];
                               }
                           });
                       });
    
    }
    

    For example:

    [self dispatchRepeat:5 withBlock:^(RepeatCompletionHandler completionHandler) {
    
        [tagsService getTagValueForTagName:TagName value:^(NSString *tagValue) {
            if (![TagValue isEqualToString:tagValue]) {
                return completionHandler(YES);
            }
            completionHandler(NO);
        }];
    
    }];
    
    0 讨论(0)
  • 2021-01-06 04:13

    GCD already got this built in

    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    if (timer) {
        dispatch_source_set_timer(timer, dispatch_time(DISPATCH_TIME_NOW, interval * NSEC_PER_SEC), interval * NSEC_PER_SEC, (1ull * NSEC_PER_SEC) / 10);
        dispatch_source_set_event_handler(timer, block);
        dispatch_resume(timer);
    }
    

    https://gist.github.com/maicki/7622108

    0 讨论(0)
  • 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).

    0 讨论(0)
提交回复
热议问题