iOS:Timer key change from NSString to NSNumber

This commit is contained in:
pengfeizhou 2021-02-23 15:58:49 +08:00 committed by osborn
parent f721713615
commit cc1b4d92cb

View File

@ -42,7 +42,7 @@ - (void)onLog:(DoricLogType)type message:(NSString *)message {
@end @end
@interface DoricJSEngine () @interface DoricJSEngine ()
@property(nonatomic, strong) NSMutableDictionary *timers; @property(nonatomic, strong) NSMutableDictionary <NSNumber *, NSTimer *> *timers;
@property(nonatomic, strong) DoricBridgeExtension *bridgeExtension; @property(nonatomic, strong) DoricBridgeExtension *bridgeExtension;
@property(nonatomic, strong) NSDictionary *innerEnvironmentDictionary; @property(nonatomic, strong) NSDictionary *innerEnvironmentDictionary;
@property(nonatomic, strong) NSThread *jsThread; @property(nonatomic, strong) NSThread *jsThread;
@ -170,19 +170,18 @@ - (void)initJSExecutor {
[self.jsExecutor injectGlobalJSObject:INJECT_TIMER_SET [self.jsExecutor injectGlobalJSObject:INJECT_TIMER_SET
obj:^(NSNumber *timerId, NSNumber *interval, NSNumber *isInterval) { obj:^(NSNumber *timerId, NSNumber *interval, NSNumber *isInterval) {
__strong typeof(_self) self = _self; __strong typeof(_self) self = _self;
NSString *timerId_str = [timerId stringValue];
BOOL repeat = [isInterval boolValue]; BOOL repeat = [isInterval boolValue];
NSTimer *timer = [NSTimer timerWithTimeInterval:[interval doubleValue] / 1000 target:self selector:@selector(callbackTimer:) userInfo:@{@"timerId": timerId, @"repeat": isInterval} repeats:repeat]; NSTimer *timer = [NSTimer timerWithTimeInterval:[interval doubleValue] / 1000 target:self selector:@selector(callbackTimer:) userInfo:@{@"timerId": timerId, @"repeat": isInterval} repeats:repeat];
[self.timers setValue:timer forKey:timerId_str]; self.timers[timerId] = timer;
dispatch_async(dispatch_get_main_queue(), ^() { dispatch_async(dispatch_get_main_queue(), ^() {
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes]; [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
}); });
}]; }];
[self.jsExecutor injectGlobalJSObject:INJECT_TIMER_CLEAR [self.jsExecutor injectGlobalJSObject:INJECT_TIMER_CLEAR
obj:^(NSString *timerId) { obj:^(NSNumber *timerId) {
__strong typeof(_self) self = _self; __strong typeof(_self) self = _self;
NSTimer *timer = [self.timers valueForKey:timerId]; NSTimer *timer = self.timers[timerId];
if (timer) { if (timer) {
[self.timers removeObjectForKey:timerId]; [self.timers removeObjectForKey:timerId];
dispatch_async(dispatch_get_main_queue(), ^{ dispatch_async(dispatch_get_main_queue(), ^{
@ -284,7 +283,7 @@ - (void)callbackTimer:(NSTimer *)timer {
message:[NSString stringWithFormat:@"Timer Callback error:%@", exception.reason]]; message:[NSString stringWithFormat:@"Timer Callback error:%@", exception.reason]];
} }
if (![repeat boolValue]) { if (![repeat boolValue]) {
[self.timers removeObjectForKey:[timerId stringValue]]; [self.timers removeObjectForKey:timerId];
} }
}]; }];
} }