|
|
@@ -8,9 +8,13 @@
|
|
|
|
|
|
#import "JKTimerManager.h"
|
|
|
|
|
|
+#define lockName(var) [NSString stringWithFormat:@"%@_canceller",var]
|
|
|
+#define timerLock(var) [[JKTimerManager sharedTimerManager].lockContainer objectForKey:lockName(var)]
|
|
|
+
|
|
|
@interface JKTimerManager ()
|
|
|
|
|
|
@property (nonatomic,strong) NSMutableDictionary *timerContainer;
|
|
|
+@property (nonatomic,strong) NSMutableDictionary *lockContainer;
|
|
|
|
|
|
@end
|
|
|
|
|
|
@@ -50,27 +54,47 @@
|
|
|
[self.timerContainer setObject:timer forKey:name];
|
|
|
}
|
|
|
|
|
|
+ NSLock *timer_lock = [[NSLock alloc] init];
|
|
|
+ [self.lockContainer setObject:timer_lock forKey:lockName(name)];
|
|
|
+
|
|
|
dispatch_source_set_timer(timer, dispatch_time(DISPATCH_TIME_NOW, timerInterval * NSEC_PER_SEC), timerInterval * NSEC_PER_SEC, 0.1 * NSEC_PER_SEC);
|
|
|
__weak typeof(self) weakSelf = self;
|
|
|
+
|
|
|
dispatch_source_set_event_handler(timer, ^{
|
|
|
- if (action) {
|
|
|
+
|
|
|
+ [timerLock(name) lock];
|
|
|
+
|
|
|
+ if (timer && action) {
|
|
|
+
|
|
|
action();
|
|
|
if (!repeats) {
|
|
|
[weakSelf cancelTimerWithName:name];
|
|
|
}
|
|
|
+
|
|
|
}
|
|
|
+
|
|
|
+ [timerLock(name) unlock];
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
- (void)cancelTimerWithName:(NSString *)name {
|
|
|
+
|
|
|
dispatch_source_t timer = [self.timerContainer objectForKey:name];
|
|
|
if (!timer) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
+ NSLock *timer_lock = timerLock(name);
|
|
|
+ [timer_lock lock];
|
|
|
+
|
|
|
+ [self.lockContainer removeObjectForKey:lockName(name)];
|
|
|
[self.timerContainer removeObjectForKey:name];
|
|
|
dispatch_source_cancel(timer);
|
|
|
+ timer = nil;
|
|
|
+
|
|
|
+ [timer_lock unlock];
|
|
|
+
|
|
|
}
|
|
|
|
|
|
- (void)cancelAllTimer {
|