Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Creating a timer with Grand Central Dispatch

##Creating a timer with Grand Central Dispatch

At the following is the implementation file of a sample class that shows, how to make a timer with the help of Grand Central Dispatch. The timer fires on a global queue, just change the queue to the main queue or any custom queue and the timer fires on this queue and not on the global queue anymore.

#import <Foundation/Foundation.h>

@interface SampleClass : NSObject
- (void)startTimer;
- (void)cancelTimer;
@end


#import "SampleClass.h"

dispatch_source_t CreateDispatchTimer(double interval, dispatch_queue_t queue, dispatch_block_t block)
{
    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);
    }
    return timer;
}


@implementation SampleClass {
    dispatch_source_t _timer;
}

- (void)startTimer
{
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    double secondsToFire = 1.000f;

    _timer = CreateDispatchTimer(secondsToFire, queue, ^{
        // Do something
    });
}

- (void)cancelTimer
{
    if (_timer) {
        dispatch_source_cancel(_timer);
        // Remove this if you are on a Deployment Target of iOS6 or OSX 10.8 and above
        dispatch_release(_timer);
        _timer = nil;
    }
}

@end
@free1990

This comment has been minimized.

Copy link

commented May 11, 2015

Great job, But this code doesn't work in my project, how make it repeat, any idea?

@lzell

This comment has been minimized.

Copy link

commented May 23, 2015

Nice! @free1990, did you make sure you created storage for the timer? See the instance var directly under @implementation

@sunny131984

This comment has been minimized.

Copy link

commented Jun 27, 2015

Its such nice code thanks for great contribution.
Guyz i need to do schedule some for some event and it call on every 24 hrs at specified time. I try to search lots of thing and i don't want to use NStimer because it consumes more battery. may i use this code to call some method on every day in background. I tried lot can you please suggest me something which not consumes my memory and works as i want.

@phatmann

This comment has been minimized.

Copy link

commented Aug 4, 2015

@sunny131984, use a scheduled local notification

@sk29110

This comment has been minimized.

Copy link

commented Sep 20, 2015

Is it possible to run the timer when application in sleep(activate another app)?

@kgoedecke

This comment has been minimized.

Copy link

commented Sep 23, 2016

@lzell good hint ;) thanks

@holmesrg

This comment has been minimized.

Copy link

commented Mar 1, 2017

You're a legend - this makes my code so much more readable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.