Skip to content

Commit

Permalink
Clear Fresco memory cache in onHostDestroy
Browse files Browse the repository at this point in the history
Summary: `onHostDestroy` is when the app is backgrounded and all Activities are destroyed. At this point it's safe to clear Fresco's memory cache.

Reviewed By: AaaChiuuu

Differential Revision: D4674950

fbshipit-source-id: f6db658ad56e3ad843cd2acfa31e2df1c40d3279
  • Loading branch information
foghina authored and facebook-github-bot committed Mar 10, 2017
1 parent 75e783d commit d9ae27b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.facebook.imagepipeline.backends.okhttp3.OkHttpImagePipelineConfigFactory;
import com.facebook.imagepipeline.core.ImagePipelineConfig;
import com.facebook.imagepipeline.listener.RequestListener;
import com.facebook.react.bridge.LifecycleEventListener;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
Expand All @@ -41,20 +42,35 @@
*/
@ReactModule(name = "FrescoModule")
public class FrescoModule extends ReactContextBaseJavaModule implements
ModuleDataCleaner.Cleanable {
ModuleDataCleaner.Cleanable, LifecycleEventListener {

private final boolean mClearOnDestroy;
private @Nullable ImagePipelineConfig mConfig;

private static boolean sHasBeenInitialized = false;

/**
* Create a new Fresco module with a default configuration (or the previously given
* configuration via {@link #FrescoModule(ReactApplicationContext, ImagePipelineConfig)}.
* configuration via {@link #FrescoModule(ReactApplicationContext, boolean, ImagePipelineConfig)}.
*
* @param reactContext the context to use
*/
public FrescoModule(ReactApplicationContext reactContext) {
this(reactContext, null);
this(reactContext, true, null);
}

/**
* Create a new Fresco module with a default configuration (or the previously given
* configuration via {@link #FrescoModule(ReactApplicationContext, boolean, ImagePipelineConfig)}.
*
* @param clearOnDestroy whether to clear the memory cache in onHostDestroy: this should be
* {@code true} for pure RN apps and {@code false} for apps that use Fresco outside of RN
* as well
* @param reactContext the context to use
*
*/
public FrescoModule(ReactApplicationContext reactContext, boolean clearOnDestroy) {
this(reactContext, clearOnDestroy, null);
}

/**
Expand All @@ -65,16 +81,24 @@ public FrescoModule(ReactApplicationContext reactContext) {
* Otherwise, the given Fresco configuration will be ignored.
*
* @param reactContext the context to use
* @param clearOnDestroy whether to clear the memory cache in onHostDestroy: this should be
* {@code true} for pure RN apps and {@code false} for apps that use Fresco outside of RN
* as well
* @param config the Fresco configuration, which will only be used for the first initialization
*/
public FrescoModule(ReactApplicationContext reactContext, @Nullable ImagePipelineConfig config) {
public FrescoModule(
ReactApplicationContext reactContext,
boolean clearOnDestroy,
@Nullable ImagePipelineConfig config) {
super(reactContext);
mClearOnDestroy = clearOnDestroy;
mConfig = config;
}

@Override
public void initialize() {
super.initialize();
getReactApplicationContext().addLifecycleEventListener(this);
if (!hasBeenInitialized()) {
// Make sure the SoLoaderShim is configured to use our loader for native libraries.
// This code can be removed if using Fresco from Maven rather than from source
Expand Down Expand Up @@ -145,6 +169,24 @@ public static ImagePipelineConfig.Builder getDefaultConfigBuilder(ReactContext c
.setRequestListeners(requestListeners);
}

@Override
public void onHostResume() {
}

@Override
public void onHostPause() {
}

@Override
public void onHostDestroy() {
// According to the javadoc for LifecycleEventListener#onHostDestroy, this is only called when
// the 'last' ReactActivity is being destroyed, which effectively means the app is being
// backgrounded.
if (mClearOnDestroy) {
Fresco.getImagePipeline().clearMemoryCaches();
}
}

private static class FrescoHandler implements SoLoaderShim.Handler {
@Override
public void loadLibrary(String libraryName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public NativeModule get() {
new ModuleSpec(FrescoModule.class, new Provider<NativeModule>() {
@Override
public NativeModule get() {
return new FrescoModule(context, mConfig != null ? mConfig.getFrescoConfig() : null);
return new FrescoModule(context, true, mConfig != null ? mConfig.getFrescoConfig() : null);
}
}),
new ModuleSpec(I18nManagerModule.class, new Provider<NativeModule>() {
Expand Down

0 comments on commit d9ae27b

Please sign in to comment.