diff --git a/doric-android/doric/src/main/java/pub/doric/DoricContext.java b/doric-android/doric/src/main/java/pub/doric/DoricContext.java index 17dc4edb..29eb94a6 100644 --- a/doric-android/doric/src/main/java/pub/doric/DoricContext.java +++ b/doric-android/doric/src/main/java/pub/doric/DoricContext.java @@ -35,6 +35,7 @@ import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; +import java.util.WeakHashMap; import java.util.concurrent.Callable; import pub.doric.async.AsyncResult; @@ -42,6 +43,7 @@ import pub.doric.navbar.IDoricNavBar; import pub.doric.navigator.IDoricNavigator; import pub.doric.performance.DoricPerformanceProfile; import pub.doric.plugin.DoricJavaPlugin; +import pub.doric.resource.DoricResource; import pub.doric.shader.RootNode; import pub.doric.shader.ViewNode; import pub.doric.utils.DoricConstant; @@ -66,6 +68,7 @@ public class DoricContext { private final Map>> mHeadNodes = new HashMap<>(); private final DoricPerformanceProfile performanceProfile; private final Map animators = new HashMap<>(); + private final Map cachedResources = new WeakHashMap<>(); public Collection> allHeadNodes(String type) { Map> headNode = mHeadNodes.get(type); @@ -216,6 +219,7 @@ public class DoricContext { javaPlugin.onTearDown(); } mPluginMap.clear(); + cachedResources.clear(); return null; } }, ThreadMode.UI); @@ -355,4 +359,12 @@ public class DoricContext { public void removeAnimator(String animatorId) { animators.remove(animatorId); } + + public void cacheResource(String resId, DoricResource doricResource) { + this.cachedResources.put(resId, doricResource); + } + + public DoricResource getCachedResource(String resId) { + return this.cachedResources.get(resId); + } } diff --git a/doric-android/doric/src/main/java/pub/doric/resource/DoricResourceManager.java b/doric-android/doric/src/main/java/pub/doric/resource/DoricResourceManager.java index 1109749c..97cde885 100644 --- a/doric-android/doric/src/main/java/pub/doric/resource/DoricResourceManager.java +++ b/doric-android/doric/src/main/java/pub/doric/resource/DoricResourceManager.java @@ -19,7 +19,6 @@ import com.github.pengfeizhou.jscore.JSObject; import java.util.HashMap; import java.util.Map; -import java.util.WeakHashMap; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -32,7 +31,6 @@ import pub.doric.DoricContext; */ public class DoricResourceManager { private final Map mResourceLoaders = new HashMap<>(); - private final Map cachedResources = new WeakHashMap<>(); public synchronized void registerLoader(DoricResourceLoader loader) { mResourceLoaders.put(loader.resourceType(), loader); @@ -48,7 +46,7 @@ public class DoricResourceManager { String resId = resource.getProperty("resId").asString().value(); String type = resource.getProperty("type").asString().value(); String identifier = resource.getProperty("identifier").asString().value(); - DoricResource doricResource = cachedResources.get(resId); + DoricResource doricResource = doricContext.getCachedResource(resId); if (doricResource == null) { if ("arrayBuffer".equals(type)) { doricResource = new DoricArrayBufferResource( @@ -59,7 +57,7 @@ public class DoricResourceManager { DoricResourceLoader loader = mResourceLoaders.get(type); if (loader != null) { doricResource = loader.load(doricContext, identifier); - cachedResources.put(resId, doricResource); + doricContext.cacheResource(resId, doricResource); } } } diff --git a/doric-iOS/Pod/Classes/DoricContext.h b/doric-iOS/Pod/Classes/DoricContext.h index 3a9e9060..c895e577 100644 --- a/doric-iOS/Pod/Classes/DoricContext.h +++ b/doric-iOS/Pod/Classes/DoricContext.h @@ -48,6 +48,7 @@ NS_ASSUME_NONNULL_BEGIN @property(nonatomic, copy) NSString *extra; @property(nonatomic, assign) BOOL destroyed; @property(nonatomic, strong) DoricPerformanceProfile *performanceProfile; +@property(nonatomic, strong) NSMapTable *cachedResources; - (instancetype)initWithScript:(NSString *)script source:(NSString *)source extra:(NSString *)extra; diff --git a/doric-iOS/Pod/Classes/DoricContext.m b/doric-iOS/Pod/Classes/DoricContext.m index b32bb011..79af29c9 100644 --- a/doric-iOS/Pod/Classes/DoricContext.m +++ b/doric-iOS/Pod/Classes/DoricContext.m @@ -42,6 +42,9 @@ - (instancetype)initWithScript:(NSString *)script source:(NSString *)source extr _headNodes = [NSMutableDictionary new]; DoricRootNode *rootNode = [[DoricRootNode alloc] initWithContext:self]; _rootNode = rootNode; + _cachedResources = [[NSMapTable alloc] initWithKeyOptions:NSPointerFunctionsCopyIn + valueOptions:NSPointerFunctionsWeakMemory + capacity:0]; [self init:extra]; [self callEntity:DORIC_ENTITY_CREATE withArgumentsArray:@[]]; } @@ -66,6 +69,7 @@ - (void)dealloc { [[DoricContextManager instance] destroyContext:self]; [self callEntity:DORIC_ENTITY_DESTROY withArgumentsArray:@[]]; [self.driver destroyContext:self.contextId]; + [self.cachedResources removeAllObjects]; } - (DoricAsyncResult *)callEntity:(NSString *)method, ... { diff --git a/doric-iOS/Pod/Classes/Resource/DoricResourceManager.m b/doric-iOS/Pod/Classes/Resource/DoricResourceManager.m index 54be1830..2c29cb48 100644 --- a/doric-iOS/Pod/Classes/Resource/DoricResourceManager.m +++ b/doric-iOS/Pod/Classes/Resource/DoricResourceManager.m @@ -18,10 +18,10 @@ // #import "DoricResourceManager.h" +#import "DoricContext.h" @interface DoricResourceManager () @property(nonatomic, strong) NSMutableDictionary > *loaders; -@property(nonatomic, strong) NSMapTable *cachedResources; @property(nonatomic, strong) dispatch_queue_t mapQueue; @end @@ -30,9 +30,6 @@ - (instancetype)init { if (self = [super init]) { _loaders = [NSMutableDictionary new]; _mapQueue = dispatch_queue_create("doric.resource", DISPATCH_QUEUE_SERIAL); - _cachedResources = [[NSMapTable alloc] initWithKeyOptions:NSPointerFunctionsCopyIn - valueOptions:NSPointerFunctionsWeakMemory - capacity:0]; } return self; } @@ -56,11 +53,11 @@ - (__kindof DoricResource *)load:(NSDictionary *)resource NSString *resId = resource[@"resId"]; __block __kindof DoricResource *doricResource; dispatch_sync(self.mapQueue, ^() { - doricResource = [self.cachedResources objectForKey:resId]; + doricResource = [context.cachedResources objectForKey:resId]; if (!doricResource) { id loader = self.loaders[type]; doricResource = [loader load:identifier withContext:context]; - [self.cachedResources setObject:doricResource forKey:resId]; + [context.cachedResources setObject:doricResource forKey:resId]; } }); return doricResource;