Skip to content

Commit 5f65383

Browse files
patrickkempffDylanVann
authored andcommitted
fix: Fix local resource cache issue on Android. (#472)
fix #402
1 parent 1eed575 commit 5f65383

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

android/src/main/java/com/dylanvann/fastimage/FastImageViewConverter.java

+23-7
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import com.bumptech.glide.load.model.Headers;
1717
import com.bumptech.glide.load.model.LazyHeaders;
1818
import com.bumptech.glide.request.RequestOptions;
19+
import com.bumptech.glide.signature.ApplicationVersionSignature;
1920
import com.facebook.react.bridge.JSApplicationIllegalArgumentException;
2021
import com.facebook.react.bridge.NoSuchKeyException;
2122
import com.facebook.react.bridge.ReadableMap;
@@ -30,6 +31,8 @@
3031

3132
import javax.annotation.Nullable;
3233

34+
import static com.bumptech.glide.request.RequestOptions.signatureOf;
35+
3336
class FastImageViewConverter {
3437
private static final Drawable TRANSPARENT_DRAWABLE = new ColorDrawable(Color.TRANSPARENT);
3538

@@ -81,7 +84,7 @@ static Headers getHeaders(ReadableMap source) {
8184
return headers;
8285
}
8386

84-
static RequestOptions getOptions(ReadableMap source) {
87+
static RequestOptions getOptions(Context context, FastImageSource imageSource, ReadableMap source) {
8588
// Get priority.
8689
final Priority priority = FastImageViewConverter.getPriority(source);
8790
// Get cache control method.
@@ -102,12 +105,25 @@ static RequestOptions getOptions(ReadableMap source) {
102105
// Use defaults.
103106
break;
104107
}
105-
return new RequestOptions()
106-
.diskCacheStrategy(diskCacheStrategy)
107-
.onlyRetrieveFromCache(onlyFromCache)
108-
.skipMemoryCache(skipMemoryCache)
109-
.priority(priority)
110-
.placeholder(TRANSPARENT_DRAWABLE);
108+
109+
RequestOptions options = new RequestOptions()
110+
.diskCacheStrategy(diskCacheStrategy)
111+
.onlyRetrieveFromCache(onlyFromCache)
112+
.skipMemoryCache(skipMemoryCache)
113+
.priority(priority)
114+
.placeholder(TRANSPARENT_DRAWABLE);
115+
116+
if (imageSource.isResource()) {
117+
// Every local resource (drawable) in Android has its own unique numeric id, which are
118+
// generated at build time. Although these ids are unique, they are not guaranteed unique
119+
// across builds. The underlying glide implementation caches these resources. To make
120+
// sure the cache does not return the wrong image, we should clear the cache when the
121+
// application version changes. Adding a cache signature for only these local resources
122+
// solves this issue: https://github.com/DylanVann/react-native-fast-image/issues/402
123+
options = options.apply(signatureOf(ApplicationVersionSignature.obtain(context)));
124+
}
125+
126+
return options;
111127
}
112128

113129
private static FastImageCacheControl getCacheControl(ReadableMap source) {

android/src/main/java/com/dylanvann/fastimage/FastImageViewManager.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public void setSrc(FastImageViewWithUrl view, @Nullable ReadableMap source) {
103103
// - android.resource://
104104
// - data:image/png;base64
105105
.load(imageSource.getSourceForLoad())
106-
.apply(FastImageViewConverter.getOptions(source))
106+
.apply(FastImageViewConverter.getOptions(context, imageSource, source))
107107
.listener(new FastImageRequestListener(key))
108108
.into(view);
109109
}

android/src/main/java/com/dylanvann/fastimage/FastImageViewModule.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public void run() {
4747
imageSource.isBase64Resource() ? imageSource.getSource() :
4848
imageSource.isResource() ? imageSource.getUri() : imageSource.getGlideUrl()
4949
)
50-
.apply(FastImageViewConverter.getOptions(source))
50+
.apply(FastImageViewConverter.getOptions(activity, imageSource, source))
5151
.preload();
5252
}
5353
}

0 commit comments

Comments
 (0)