dispatch_after looped / repeated

心不动则不痛 提交于 2019-12-19 03:57:01

问题


I am trying to create a loop like this:

while (TRUE){
  dispatch_after(...{
    <some action>
  });
}

After a viewDidLoad. The idea is to repeat the dispatch_after repeatedly. The dispatch_after waits two seconds before doing the action.

This does not work - the screen just blanks? Is it stuck in looping or ...?


回答1:


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




回答2:


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




回答3:


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




回答4:


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);
    }];

}];


来源:https://stackoverflow.com/questions/11465084/dispatch-after-looped-repeated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!