Skip to content

Instantly share code, notes, and snippets.

@srpoucse
Last active August 29, 2015 14:03
Show Gist options
  • Select an option

  • Save srpoucse/cf0c85113498dd24aa6f to your computer and use it in GitHub Desktop.

Select an option

Save srpoucse/cf0c85113498dd24aa6f to your computer and use it in GitHub Desktop.

Revisions

  1. srpoucse renamed this gist Jul 8, 2014. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion gistfile1.txt → Timer with Grand Central Dispatch
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,3 @@

    #define TIME_INTERVAL_TO_FIRE 15 * NSEC_PER_SEC

    #import <Foundation/Foundation.h>
  2. srpoucse created this gist Jul 8, 2014.
    49 changes: 49 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,49 @@

    #define TIME_INTERVAL_TO_FIRE 15 * NSEC_PER_SEC

    #import <Foundation/Foundation.h>

    @interface SampleClass : NSObject
    {
    dispatch_source_t timer;
    }

    - (void)startTimer;
    - (void)cancelTimer;

    @end


    #import "SampleClass.h"


    @implementation SampleClass {
    dispatch_source_t _timer;
    }

    - (void)startTimer
    {
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0);
    timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    dispatch_source_set_timer(timer, DISPATCH_TIME_NOW,TIME_INTERVAL_TO_FIRE , (1ul * NSEC_PER_SEC)/10);
    dispatch_source_set_event_handler(timer, ^
    {
    //do something
    });


    dispatch_resume(timer);


    }

    - (void)cancelTimer
    {
    if (timer) {
    dispatch_source_cancel(timer);
    timer = nil;
    }
    }

    @end