summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoel Fischer <joeljfischer@gmail.com>2017-09-12 15:38:13 -0400
committerGitHub <noreply@github.com>2017-09-12 15:38:13 -0400
commitc9f55e17251650a1330d82d024e7eb739f81c20c (patch)
treeec6b896678fc1151f6a30c61fb05a399a42c6bc0
parente94a530047aa0251c997339d7408847d87b96701 (diff)
parent5e421045fa5d96939add75cbe673191bbe7b94b2 (diff)
downloadsdl_ios-c9f55e17251650a1330d82d024e7eb739f81c20c.tar.gz
Merge pull request #710 from cromotron/bugfix/issue_382_timer_is_not_deallocated
Fix #382 Timers are not appropriately stopped when deallocated.
-rw-r--r--SmartDeviceLink/SDLTimer.m36
1 files changed, 34 insertions, 2 deletions
diff --git a/SmartDeviceLink/SDLTimer.m b/SmartDeviceLink/SDLTimer.m
index 84ee6e24b..53a986451 100644
--- a/SmartDeviceLink/SDLTimer.m
+++ b/SmartDeviceLink/SDLTimer.m
@@ -4,8 +4,38 @@
#import "SDLTimer.h"
+@protocol SDLTimerTargetDelegate <NSObject>
-@interface SDLTimer ()
+- (void)timerElapsed;
+
+@end
+
+@interface SDLTimerTarget : NSObject
+
+@property (nonatomic, weak) id<SDLTimerTargetDelegate> delegate;
+
+@end
+
+@implementation SDLTimerTarget
+
+- (instancetype)initWithDelegate:(id)delegate {
+ self = [super init];
+ if (self) {
+ _delegate = delegate;
+ }
+ return self;
+}
+
+- (void)timerElapsed {
+ if ([self.delegate conformsToProtocol:@protocol(SDLTimerTargetDelegate)]) {
+ [_delegate timerElapsed];
+ }
+}
+
+@end
+
+
+@interface SDLTimer () <SDLTimerTargetDelegate>
@property (strong) NSTimer *timer;
@property (assign) BOOL timerRunning;
@@ -40,7 +70,9 @@
- (void)start {
if (self.duration > 0) {
[self stopAndDestroyTimer];
- self.timer = [NSTimer timerWithTimeInterval:self.duration target:self selector:@selector(timerElapsed) userInfo:nil repeats:self.repeat];
+
+ SDLTimerTarget *timerTarget = [[SDLTimerTarget alloc] initWithDelegate:self];
+ self.timer = [NSTimer timerWithTimeInterval:_duration target:timerTarget selector:@selector(timerElapsed) userInfo:nil repeats:_repeat];
[[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
self.timerRunning = YES;
}