Roel Van de Paar
Roel Van de Paar

Reputation: 2228

dispatch_after looped / repeated

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

Upvotes: 4

Views: 9217

Answers (4)

hfossli
hfossli

Reputation: 22962

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

Upvotes: 11

hfossli
hfossli

Reputation: 22962

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

Upvotes: 6

Pellet
Pellet

Reputation: 2437

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

}];

Upvotes: 1

David R&#246;nnqvist
David R&#246;nnqvist

Reputation: 56625

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

Upvotes: 7

Related Questions