Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add a method for creating an interruptable timer.

  • Loading branch information...
commit 14ba144d7736438ee0071b69a3dff7aef3fd3249 1 parent 494e0a6
Abizer Nasir authored
Showing with 22 additions and 0 deletions.
  1. +2 −0  NSTimer+JCSBlocks.h
  2. +20 −0 NSTimer+JCSBlocks.m
View
2  NSTimer+JCSBlocks.h
@@ -13,9 +13,11 @@
#import <Foundation/NSTimer.h>
typedef void (^timerCallback_t)();
+typedef void (^interruptableTimerCallback_t)(BOOL *stop);
@interface NSTimer (JCSBlocks)
+ (NSTimer *)jcs_scheduledTimerWithTimeInterval:(NSTimeInterval)seconds repeats:(BOOL)repeats blockHandler:(timerCallback_t)block;
++ (NSTimer *)jcs_scheduledInterrubtableTimerWithTimeInterval:(NSTimeInterval)seconds blockHandler:(interruptableTimerCallback_t)block;
@end
View
20 NSTimer+JCSBlocks.m
@@ -11,6 +11,7 @@
@interface NSTimer (JCSBlocks_Private)
+ (void)jcs_handleBlockWithTimer:(NSTimer *)timer;
++ (void)jcs_handleBlockWithInterruptableTimer:(NSTimer *)timer;
@end
@@ -21,6 +22,10 @@ + (NSTimer *)jcs_scheduledTimerWithTimeInterval:(NSTimeInterval)seconds repeats:
}
++ (NSTimer *)jcs_scheduledInterrubtableTimerWithTimeInterval:(NSTimeInterval)seconds blockHandler:(interruptableTimerCallback_t)block{
+ return [self scheduledTimerWithTimeInterval:seconds target:self selector:@selector(jcs_handleBlockWithInterruptableTimer:) userInfo:[block copy] repeats:YES];
+}
+
@end
@implementation NSTimer (JCSBlocks_Private)
@@ -29,7 +34,22 @@ + (void)jcs_handleBlockWithTimer:(NSTimer *)timer {
ZAssert(([timer isValid] && [timer userInfo]), @"the timer is not valid");
timerCallback_t block = [timer userInfo];
+
block();
}
++ (void)jcs_handleBlockWithInterruptableTimer:(NSTimer *)timer {
+ ZAssert(([timer isValid] && [timer userInfo]), @"the timer is not valid");
+
+ interruptableTimerCallback_t block = [timer userInfo];
+
+ BOOL stop;
+ block(&stop);
+
+ if (stop) {
+ [timer invalidate];
+ timer = nil;
+ }
+}
+
@end
Please sign in to comment.
Something went wrong with that request. Please try again.