bumptech / bumptech/glide

Race condition when loading images into AppWidgets with AppWidgetTarget

Open
#3,348 2 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Java
Stars
35k
Forks
6.2k
Avg merge
1d 11h
Merged PRs (30d)
8

Description

**4.8.0**:

**okhttp:3.10.0**:

**Pixel 2 XL/Pie**:

**I am dynamically loading an image from a web URL into a widget via AppWidgetTarget. Depending on how long the image request takes there is a race condition as to how updates to the widget occur.

This seems to be because AppWidgetTarget updates the widget via a call to AppWidgetManager.updateAppWidget() which overwrites the widgets RemoteViews state with the passed version. If there had been any updates (elsewhere in the app) via partiallyUpdateAppWidget() then when the image is loaded by Glide those changes will get overwritten by Glide via the AppWidgetManager.updateAppWidget() call.**:

**Some code...**:
```java
// This is my current workaround that avoids Glide resetting my widget RemoteViews unpredictably.
// I override onResourceReady() but don't call super() and instead set the image resource myself
// then call partiallyUpdateAppWidget() rather than updateAppWidget()
final RemoteViews views = new RemoteViews(context.getPackageName(), layout_id);
AppWidgetTarget appWidgetTarget =
new AppWidgetTarget(context.getApplicationContext(), R.id.icon_from_currency, views, appWidgetId) {
@Override
public void onResourceReady(Bitmap resource, Transition transition) {
views.setImageViewBitmap(R.id.icon_from_currency, resource);
appWidgetManager.partiallyUpdateAppWidget(appWidgetId, views);
}
};

GlideApp
.with(context.getApplicationContext())
.asBitmap()
.load("http://example.com/image.jpg")
.into(appWidgetTarget);
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.