Last active
August 29, 2015 14:03
-
-
Save srpoucse/cf0c85113498dd24aa6f to your computer and use it in GitHub Desktop.
Creating a timer with Grand Central Dispatch - Make Sure It Runs !
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment