fix:iOS Runloop cannot exit and other issues

This commit is contained in:
pengfei.zhou 2021-03-04 12:53:42 +08:00 committed by osborn
parent 627f323ae3
commit e78625281f
6 changed files with 20 additions and 8 deletions

View File

@ -212,6 +212,7 @@ public class DoricContext {
this.script = script; this.script = script;
this.mRootNode.setId(""); this.mRootNode.setId("");
this.mRootNode.clearSubModel(); this.mRootNode.clearSubModel();
this.mRootNode.getView().removeAllViews();
getDriver().createContext(mContextId, script, source); getDriver().createContext(mContextId, script, source);
init(this.extra); init(this.extra);
callEntity(DoricConstant.DORIC_ENTITY_CREATE); callEntity(DoricConstant.DORIC_ENTITY_CREATE);

View File

@ -205,9 +205,9 @@ - (void)startDebugging:(NSString *)source {
[self.wsClient sendToDebugger:@"DEBUG_RES" payload:@{ [self.wsClient sendToDebugger:@"DEBUG_RES" payload:@{
@"contextId": context.contextId @"contextId": context.contextId
}]; }];
dispatch_async(dispatch_get_main_queue(), ^{
self.debuggable = [[DoricContextDebuggable alloc] initWithWSClient:self.wsClient context:context]; self.debuggable = [[DoricContextDebuggable alloc] initWithWSClient:self.wsClient context:context];
[self.debuggable startDebug]; [self.debuggable startDebug];
dispatch_async(dispatch_get_main_queue(), ^{
for (id <DoricDevStatusCallback> callback in self.callbacks) { for (id <DoricDevStatusCallback> callback in self.callbacks) {
[callback onStartDebugging:context]; [callback onStartDebugging:context];
} }
@ -224,9 +224,9 @@ - (void)stopDebugging:(BOOL)resume {
[self.wsClient sendToDebugger:@"DEBUG_STOP" payload:@{ [self.wsClient sendToDebugger:@"DEBUG_STOP" payload:@{
@"msg": @"Stop debugging" @"msg": @"Stop debugging"
}]; }];
dispatch_async(dispatch_get_main_queue(), ^{
[self.debuggable stopDebug:resume]; [self.debuggable stopDebug:resume];
self.debuggable = nil; self.debuggable = nil;
dispatch_async(dispatch_get_main_queue(), ^{
for (id <DoricDevStatusCallback> callback in self.callbacks) { for (id <DoricDevStatusCallback> callback in self.callbacks) {
[callback onStopDebugging]; [callback onStopDebugging];
} }

View File

@ -52,6 +52,7 @@ @interface DoricRemoteJSExecutor () <DoricWSClientInterceptor>
@property(nonatomic) NSInteger counter; @property(nonatomic) NSInteger counter;
@property(nonatomic, strong) NSMutableDictionary <NSNumber *, dispatch_semaphore_t> *semaphores; @property(nonatomic, strong) NSMutableDictionary <NSNumber *, dispatch_semaphore_t> *semaphores;
@property(nonatomic, strong) NSMutableDictionary <NSNumber *, JSValue *> *results; @property(nonatomic, strong) NSMutableDictionary <NSNumber *, JSValue *> *results;
@property(nonatomic, strong) JSContext *jsContext;
@end @end
@implementation DoricRemoteJSExecutor @implementation DoricRemoteJSExecutor
@ -63,6 +64,7 @@ - (instancetype)initWithWSClient:(DoricWSClient *)wsClient {
_semaphores = [NSMutableDictionary new]; _semaphores = [NSMutableDictionary new];
_results = [NSMutableDictionary new]; _results = [NSMutableDictionary new];
_counter = 0; _counter = 0;
_jsContext = [[JSContext alloc] init];
} }
return self; return self;
} }
@ -128,7 +130,7 @@ - (JSValue *)invokeObject:(NSString *)objName method:(NSString *)funcName args:(
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
self.semaphores[@(callId)] = semaphore; self.semaphores[@(callId)] = semaphore;
self.invokingMethod = YES; self.invokingMethod = YES;
DoricLog(@"Lock %@",@(callId)); DoricLog(@"Lock %@", @(callId));
DC_LOCK(semaphore); DC_LOCK(semaphore);
JSValue *result = self.results[@(callId)]; JSValue *result = self.results[@(callId)];
[self.results removeObjectForKey:@(callId)]; [self.results removeObjectForKey:@(callId)];
@ -169,7 +171,9 @@ - (BOOL)interceptType:(NSString *)type command:(NSString *)cmd payload:(NSDictio
NSString *name = payload[@"name"]; NSString *name = payload[@"name"];
NSArray *argsArr = payload[@"arguments"]; NSArray *argsArr = payload[@"arguments"];
id tmpBlk = self.blockMDic[name]; id tmpBlk = self.blockMDic[name];
if (argsArr.count == 0) { if (!tmpBlk) {
DoricLog(@"Cannot find inject function:%@", name);
} else if (argsArr.count == 0) {
((Block0) tmpBlk)(); ((Block0) tmpBlk)();
} else if (argsArr.count == 1) { } else if (argsArr.count == 1) {
((Block1) tmpBlk)(argsArr[0]); ((Block1) tmpBlk)(argsArr[0]);
@ -187,12 +191,12 @@ - (BOOL)interceptType:(NSString *)type command:(NSString *)cmd payload:(NSDictio
} else if ([cmd isEqualToString:@"invokeMethod"]) { } else if ([cmd isEqualToString:@"invokeMethod"]) {
NSNumber *callId = payload[@"callId"]; NSNumber *callId = payload[@"callId"];
@try { @try {
JSValue *value = [JSValue valueWithObject:payload[@"result"] inContext:nil]; JSValue *value = [JSValue valueWithObject:payload[@"result"] inContext:self.jsContext];
self.results[callId] = value; self.results[callId] = value;
} @catch (NSException *exception) { } @catch (NSException *exception) {
DoricLog(@"debugger ", NSStringFromSelector(_cmd), exception.reason); DoricLog(@"debugger ", NSStringFromSelector(_cmd), exception.reason);
} @finally { } @finally {
DoricLog(@"Unlock:%@",payload); DoricLog(@"Unlock:%@", payload);
dispatch_semaphore_t semaphore = self.semaphores[callId]; dispatch_semaphore_t semaphore = self.semaphores[callId];
[self.semaphores removeObjectForKey:callId]; [self.semaphores removeObjectForKey:callId];
DC_UNLOCK(semaphore); DC_UNLOCK(semaphore);

View File

@ -38,6 +38,7 @@ - (instancetype)initWithUrl:(NSString *)url {
_interceptors = [NSHashTable hashTableWithOptions:NSPointerFunctionsWeakMemory]; _interceptors = [NSHashTable hashTableWithOptions:NSPointerFunctionsWeakMemory];
_websocket = [[SRWebSocket alloc] initWithURL:[NSURL URLWithString:url]]; _websocket = [[SRWebSocket alloc] initWithURL:[NSURL URLWithString:url]];
_websocket.delegate = self; _websocket.delegate = self;
_websocket.delegateDispatchQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
[_websocket open]; [_websocket open];
} }
return self; return self;

View File

@ -102,6 +102,9 @@ - (void)reload:(NSString *)script {
[self.driver destroyContext:self.contextId]; [self.driver destroyContext:self.contextId];
self.rootNode.viewId = nil; self.rootNode.viewId = nil;
[self.rootNode clearSubModel]; [self.rootNode clearSubModel];
[self.rootNode.view.subviews forEach:^(__kindof UIView *obj) {
[obj removeFromSuperview];
}];
self.script = script; self.script = script;
[self.driver createContext:self.contextId script:script source:self.source]; [self.driver createContext:self.contextId script:script source:self.source];
[self init:self.extra]; [self init:self.extra];

View File

@ -103,6 +103,9 @@ - (instancetype)init {
- (void)teardown { - (void)teardown {
_destroyed = YES; _destroyed = YES;
//To ensure runloop continue.
[self ensureRunOnJSThread:^{
}];
} }
- (void)ensureRunOnJSThread:(dispatch_block_t)block { - (void)ensureRunOnJSThread:(dispatch_block_t)block {