HMTimer
HMTimer is an Objective C wrapper that provides a simple, NSTimer-like interface for dispatch_after() with a block completion handler.
Usage
Unscheduled instances of HMTimer must be fired manually:
|
1 2 3 4 5 6 7 8 |
HMTimer *timer = [HMTimer timerWithDuration: 2.0
queue: dispatch_get_main_queue()
repeats: NO
completionHandler: ^(void) {
// Do whatever you want upon completion...
}];
[timer fire]; |
Scheduled instances of HMTimer are fired automatically upon initialization:
|
1 2 3 4 5 6 |
HMTimer *timer = [HMTimer scheduledTimerWithDuration: 2.0
queue: dispatch_get_main_queue()
repeats: NO
completionHandler: ^(void) {
// Do whatever you want upon completion...
}]; |
If the repeats parameter is true, the timer will fire repeatedly using the duration as the interval between firings:
|
1 2 3 4 5 6 |
HMTimer *timer = [HMTimer scheduledTimerWithDuration: 2.0
queue: dispatch_get_main_queue()
repeats: YES
completionHandler: ^(void) {
// Do whatever you want upon completion...
}]; |
You can specify a different dispatch queue to keep HMTimer from running on the main thread:
|
1 2 3 4 5 6 7 8 |
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
HMTimer *timer = [HMTimer scheduledTimerWithDuration: 2.0
queue: queue
repeats: NO
completionHandler: ^(void) {
// Do whatever you want upon completion...
}]; |
An instance of HMTimer can be invalidated by calling the - invalidate method:
|
1 |
[timer invalidate]; |
Grab the code on Github…