You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When an image is cached to disk, it is pinned to its URL (and other attributes, but let's only consider the URL).
If the image pointed by the URL changes, the old cached entry is used, resulting in the wrong image displayed.
In my use case, I have additional data (a String but could be something else) associated to the URL that changes if the image pointed by the URL has changed.
So pinning cached images to URL+data instead of just URL, solves nicely the problem.
However, Glide doesn't make this easy and requires quite a bit of boiler plate code, as key building is buried inside DataFetcher.getId():
publicstaticclassCustomGlideUrlextendsGlideUrl {
Stringdata;
publicCustomGlideUrl(Stringurl, Stringdata) {
super(url);
this.data = data;
}
publicStringgetData() {
returndata;
}
}
staticpublicclassCustomOkHttpUrlLoaderimplementsModelLoader<CustomGlideUrl, InputStream> {
/** * The default factory for {@link OkHttpUrlLoader}s. */publicstaticclassFactoryimplementsModelLoaderFactory<CustomGlideUrl, InputStream> {
privatestaticvolatileOkHttpClientinternalClient;
protectedOkHttpClientclient;
protectedStringuserAgent;
privatestaticOkHttpClientgetInternalClient() {
if (internalClient == null) {
synchronized (Factory.class) {
if (internalClient == null) {
internalClient = newOkHttpClient();
}
}
}
returninternalClient;
}
/** * Constructor for a new Factory that runs requests using a static singleton client. */publicFactory(StringuserAgent) {
this(getInternalClient(), userAgent);
}
/** * Constructor for a new Factory that runs requests using given client. */publicFactory(OkHttpClientclient, StringuserAgent) {
this.client = client;
this.userAgent = userAgent;
}
@OverridepublicModelLoader<CustomGlideUrl, InputStream> build(Contextcontext, GenericLoaderFactoryfactories) {
returnnewCustomOkHttpUrlLoader(client, userAgent);
}
@Overridepublicvoidteardown() {
// Do nothing, this instance doesn't own the client.
}
}
protectedfinalOkHttpClientclient;
protectedfinalStringuserAgent;
publicCustomOkHttpUrlLoader(OkHttpClientclient, StringuserAgent) {
this.client = client;
this.userAgent = userAgent;
}
@OverridepublicDataFetcher<InputStream> getResourceFetcher(finalCustomGlideUrlmodel, intwidth, intheight) {
returnnewOkHttpStreamFetcher(client, model, userAgent) {
@OverridepublicStringgetId() {
returnsuper.getId() + model.getData();
}
};
}
}
Glide.get(this).register(CustomGlideUrl.class, InputStream.class, newCustomOkHttpUrlLoader.Factory(null));
Glide.with(this).load(newCustomGlideUrl(url, "some data that changes if image pointed by url changes")...;
It could be interesting to make custom key building easier.
The text was updated successfully, but these errors were encountered:
When an image is cached to disk, it is pinned to its URL (and other attributes, but let's only consider the URL).
If the image pointed by the URL changes, the old cached entry is used, resulting in the wrong image displayed.
In my use case, I have additional data (a String but could be something else) associated to the URL that changes if the image pointed by the URL has changed.
So pinning cached images to URL+data instead of just URL, solves nicely the problem.
However, Glide doesn't make this easy and requires quite a bit of boiler plate code, as key building is buried inside DataFetcher.getId():
It could be interesting to make custom key building easier.
The text was updated successfully, but these errors were encountered: