diff --git a/doric-iOS/Devkit/Classes/DoricDev.m b/doric-iOS/Devkit/Classes/DoricDev.m index 9c4e5f07..f39d98ec 100644 --- a/doric-iOS/Devkit/Classes/DoricDev.m +++ b/doric-iOS/Devkit/Classes/DoricDev.m @@ -111,8 +111,7 @@ - (void)onConnectExceptionEvent { - (void)onStartDebugEvent:(NSNotification *)notification { NSString *contextId = notification.object; ShowToast(contextId, DoricGravityBottom); - for (NSValue *value in [[DoricContextManager instance] aliveContexts]) { - DoricContext *context = value.nonretainedObjectValue; + for (DoricContext *context in [[DoricContextManager instance] aliveContexts]) { BOOL result = [context.contextId compare:contextId] == NSOrderedSame; if (result) { _context = context; diff --git a/doric-iOS/Devkit/Classes/DoricDevViewController.m b/doric-iOS/Devkit/Classes/DoricDevViewController.m index 91fde21b..fe877fd1 100644 --- a/doric-iOS/Devkit/Classes/DoricDevViewController.m +++ b/doric-iOS/Devkit/Classes/DoricDevViewController.m @@ -58,17 +58,18 @@ - (void)viewDidLoad { [self.navigationController pushViewController:[QRScanViewController new] animated:NO]; } } + - (void)onClose { [[DoricDev instance] closeDevMode]; [self.navigationController popViewControllerAnimated:YES]; } + - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [DoricContextManager.instance aliveContexts].count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { - NSValue *value = [DoricContextManager.instance aliveContexts][(NSUInteger) indexPath.row]; - DoricContext *context = value.nonretainedObjectValue; + DoricContext *context = [DoricContextManager.instance aliveContexts][(NSUInteger) indexPath.row]; NSString *path = context.source; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; if (cell == nil) { @@ -84,8 +85,7 @@ - (BOOL)isSimulator { } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { - NSValue *value = [DoricContextManager.instance aliveContexts][(NSUInteger) indexPath.row]; - DoricContext *context = value.nonretainedObjectValue; + DoricContext *context = [DoricContextManager.instance aliveContexts][(NSUInteger) indexPath.row]; [[NSNotificationCenter defaultCenter] postNotificationName:@"StartDebugEvent" object:context.contextId]; NSDictionary *jsonDic = @{ @"cmd": @"DEBUG", diff --git a/doric-iOS/Devkit/Classes/DoricWSClient.m b/doric-iOS/Devkit/Classes/DoricWSClient.m index 9b74b5a8..47702d1b 100644 --- a/doric-iOS/Devkit/Classes/DoricWSClient.m +++ b/doric-iOS/Devkit/Classes/DoricWSClient.m @@ -64,8 +64,7 @@ - (void)webSocket:(SRWebSocket *)webSocket didReceiveMessage:(id)message { } else if ([cmd compare:@"RELOAD"] == NSOrderedSame) { NSString *source = [[dic valueForKey:@"source"] mutableCopy]; NSString *script = [dic valueForKey:@"script"]; - for (NSValue *value in [[DoricContextManager instance] aliveContexts]) { - DoricContext *context = value.nonretainedObjectValue; + for (DoricContext *context in [[DoricContextManager instance] aliveContexts]) { if ([source containsString:context.source] || [context.source isEqualToString:@"__dev__"]) { [context reload:script]; } diff --git a/doric-iOS/Pod/Classes/DoricContextManager.h b/doric-iOS/Pod/Classes/DoricContextManager.h index d8927542..7b6faf05 100644 --- a/doric-iOS/Pod/Classes/DoricContextManager.h +++ b/doric-iOS/Pod/Classes/DoricContextManager.h @@ -34,7 +34,7 @@ NS_ASSUME_NONNULL_BEGIN - (DoricContext *)getContext:(NSString *)contextId; -- (NSArray *)aliveContexts; +- (NSArray *)aliveContexts; @end NS_ASSUME_NONNULL_END diff --git a/doric-iOS/Pod/Classes/DoricContextManager.m b/doric-iOS/Pod/Classes/DoricContextManager.m index fdf2d39f..1a23d2ad 100644 --- a/doric-iOS/Pod/Classes/DoricContextManager.m +++ b/doric-iOS/Pod/Classes/DoricContextManager.m @@ -25,7 +25,7 @@ @interface DoricContextManager () @property(nonatomic) NSInteger counter; -@property(nonatomic, strong) NSMutableDictionary *doricContextMap; +@property(nonatomic, strong) NSMapTable *contextMapTable; @property(nonatomic, strong) dispatch_queue_t mapQueue; @end @@ -33,9 +33,11 @@ @implementation DoricContextManager - (instancetype)init { if (self = [super init]) { - _doricContextMap = [[NSMutableDictionary alloc] init]; _counter = 0; _mapQueue = dispatch_queue_create("doric.contextmap", DISPATCH_QUEUE_SERIAL); + _contextMapTable = [[NSMapTable alloc] initWithKeyOptions:NSPointerFunctionsCopyIn + valueOptions:NSPointerFunctionsWeakMemory + capacity:0]; } return self; } @@ -52,8 +54,7 @@ + (instancetype)instance { - (void)createContext:(DoricContext *)context script:(NSString *)script source:(NSString *)source { context.contextId = [NSString stringWithFormat:@"%ld", (long) ++self.counter]; dispatch_sync(self.mapQueue, ^() { - NSValue *value = [NSValue valueWithNonretainedObject:context]; - self.doricContextMap[context.contextId] = value; + [self.contextMapTable setObject:context forKey:context.contextId]; }); [context.driver createContext:context.contextId script:script source:source]; } @@ -61,8 +62,7 @@ - (void)createContext:(DoricContext *)context script:(NSString *)script source:( - (DoricContext *)getContext:(NSString *)contextId { __block DoricContext *context; dispatch_sync(self.mapQueue, ^{ - NSValue *value = self.doricContextMap[contextId]; - context = value.nonretainedObjectValue; + context = [self.contextMapTable objectForKey:contextId]; }); return context; } @@ -70,12 +70,18 @@ - (DoricContext *)getContext:(NSString *)contextId { - (void)destroyContext:(DoricContext *)context { NSString *contextId = context.contextId; dispatch_sync(self.mapQueue, ^() { - [self.doricContextMap removeObjectForKey:contextId]; + [self.contextMapTable removeObjectForKey:contextId]; }); } -- (NSArray *)aliveContexts { - return [self.doricContextMap allValues]; +- (NSArray *)aliveContexts { + NSEnumerator *enumerator = [self.contextMapTable objectEnumerator]; + NSMutableArray *ret = [NSMutableArray new]; + DoricContext *context; + while ((context = [enumerator nextObject])) { + [ret addObject:context]; + } + return ret; } @end diff --git a/doric-iOS/Pod/Classes/Extension/DoricBridgeExtension.m b/doric-iOS/Pod/Classes/Extension/DoricBridgeExtension.m index 86369d01..4c996108 100644 --- a/doric-iOS/Pod/Classes/Extension/DoricBridgeExtension.m +++ b/doric-iOS/Pod/Classes/Extension/DoricBridgeExtension.m @@ -34,7 +34,7 @@ @implementation DoricBridgeExtension - (id)callNativeWithContextId:(NSString *)contextId module:(NSString *)module method:(NSString *)method callbackId:(NSString *)callbackId argument:(id)argument { __strong DoricContext *context = [[DoricContextManager instance] getContext:contextId]; - if (context.destroyed) { + if (!context || context.destroyed) { return nil; } Class pluginClass = [self.registry acquireNativePlugin:module];