feat:avoid iOS dealloc JSValue in main thread,this may cause crash in JavaScriptCore

This commit is contained in:
pengfeizhou 2021-01-06 11:56:30 +08:00 committed by osborn
parent 967ad27a22
commit 2100eff054
7 changed files with 29 additions and 8 deletions

View File

@ -23,6 +23,7 @@ @implementation ViewController
- (void)viewDidLoad { - (void)viewDidLoad {
[super viewDidLoad]; [super viewDidLoad];
self.title = @"Doric Demo"; self.title = @"Doric Demo";
[self.navigationController.navigationBar setBackgroundImage:UIImageWithColor(UIColor.whiteColor) forBarMetrics:UIBarMetricsDefault];
NSString *path = [[NSBundle mainBundle] bundlePath]; NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *demoPath = [path stringByAppendingPathComponent:@"src"]; NSString *demoPath = [path stringByAppendingPathComponent:@"src"];
NSFileManager *mgr = [NSFileManager defaultManager]; NSFileManager *mgr = [NSFileManager defaultManager];

View File

@ -237,8 +237,9 @@ - (NSDictionary *)itemModelAt:(NSUInteger)position {
return [self subModelOf:viewId]; return [self subModelOf:viewId];
} else { } else {
DoricAsyncResult *result = [self callJSResponse:@"renderBunchedItems", @(position), @(self.batchCount), nil]; DoricAsyncResult *result = [self callJSResponse:@"renderBunchedItems", @(position), @(self.batchCount), nil];
JSValue *models = [result waitUntilResult]; NSArray *array = [result waitUntilResult:^(JSValue *models) {
NSArray *array = [models toArray]; return [models toArray];
}];
[array enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL *stop) { [array enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL *stop) {
NSString *thisViewId = obj[@"id"]; NSString *thisViewId = obj[@"id"];
[self setSubModel:obj in:thisViewId]; [self setSubModel:obj in:thisViewId];

View File

@ -178,8 +178,9 @@ - (NSDictionary *)itemModelAt:(NSUInteger)position {
batchCount++; batchCount++;
} }
DoricAsyncResult *result = [self callJSResponse:@"renderBunchedItems", @(start), @(batchCount), nil]; DoricAsyncResult *result = [self callJSResponse:@"renderBunchedItems", @(start), @(batchCount), nil];
JSValue *models = [result waitUntilResult]; NSArray *array = [result waitUntilResult:^(JSValue *models) {
NSArray *array = [models toArray]; return [models toArray];
}];
[array enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL *stop) { [array enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL *stop) {
NSString *thisViewId = obj[@"id"]; NSString *thisViewId = obj[@"id"];
[self setSubModel:obj in:thisViewId]; [self setSubModel:obj in:thisViewId];

View File

@ -164,8 +164,9 @@ - (NSDictionary *)itemModelAt:(NSUInteger)position {
return [self subModelOf:viewId]; return [self subModelOf:viewId];
} else { } else {
DoricAsyncResult *result = [self callJSResponse:@"renderBunchedItems", @(index), @(self.batchCount), nil]; DoricAsyncResult *result = [self callJSResponse:@"renderBunchedItems", @(index), @(self.batchCount), nil];
JSValue *models = [result waitUntilResult]; NSArray *array = [result waitUntilResult:^(JSValue *models) {
NSArray *array = [models toArray]; return [models toArray];
}];
[array enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL *stop) { [array enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL *stop) {
NSString *thisViewId = obj[@"id"]; NSString *thisViewId = obj[@"id"];
[self setSubModel:obj in:thisViewId]; [self setSubModel:obj in:thisViewId];

View File

@ -24,7 +24,6 @@
NS_ASSUME_NONNULL_BEGIN NS_ASSUME_NONNULL_BEGIN
@interface DoricAsyncResult <R> : NSObject @interface DoricAsyncResult <R> : NSObject
@property(nonatomic, strong) void (^resultCallback)(R result); @property(nonatomic, strong) void (^resultCallback)(R result);
@property(nonatomic, strong) void (^exceptionCallback)(NSException *e); @property(nonatomic, strong) void (^exceptionCallback)(NSException *e);
@ -39,6 +38,9 @@ NS_ASSUME_NONNULL_BEGIN
- (R)getResult; - (R)getResult;
- (R)waitUntilResult; - (R)waitUntilResult;
- (id)waitUntilResult:(id (^)(R result))transformer;
@end @end
NS_ASSUME_NONNULL_END NS_ASSUME_NONNULL_END

View File

@ -89,4 +89,19 @@ - (id)waitUntilResult {
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER); dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
return self.result; return self.result;
} }
- (id)waitUntilResult:(id (^)(id result))transformer {
if (self.result) {
return transformer(self.result);
}
__block id ret = nil;
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
self.resultCallback = ^(id r) {
ret = transformer(r);
dispatch_semaphore_signal(semaphore);
};
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
return ret;
}
@end @end