bumptech / bumptech/glide

Glide Clear(View) not clearing pending requests

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

Description

**Glide Version**: 4.12.0

**Integration libraries**: okhttp3

**Device/Android Version**: Pixel 4XL emulator API 28. Device type does not seem to be related to this issue.

**Issue details / Repro steps / Use case background**:
Pending glide requests are not cancelled when making another request for the same View when using `Glide.with(context.clear(View)`.

The documentation describes `clear(View)` as the following:
```
Cancel any pending loads Glide may have for the view and free any resources that may have been loaded for the view.
Note that this will only work if View.setTag(Object) is not called on this view outside of Glide.
Params: view – The view to cancel loads and free resources for.
Throws: IllegalArgumentException – if an object other than Glide's metadata is put as the view's tag.
See Also: clear(Target)
```

The specific issue with my code is that two glide calls are made on the same view, but the old one is loading after the new one making the old view appear instead of the new view. I have tried using the clear method that is described but this does not cancel the pending request.

**Glide load line / `GlideModule` (if any) / list Adapter code (if any)**:
```kotlin
fun ImageView.fitGifOrImage(
context: Context,
imageUrl: String,
onResourceReady: () -> Unit,
onLoadFailed: (e: Exception?) -> Unit
) {
if (imageUrl.contains(".gif")) {
this.fitGif(context, imageUrl, onResourceReady, onLoadFailed)
} else {
this.fitImageGlide(context, imageUrl, onResourceReady, onLoadFailed)
}
}

fun ImageView.fitGif(
context: Context,
imageUrl: String,
onResourceReady: () -> Unit,
onLoadFailed: (e: GlideException?) -> Unit
) {
Glide.with(context)
.asGif()
.load(imageUrl)
.centerCrop()
.listener(object : RequestListener {
override fun onResourceReady(
resource: GifDrawable?,
model: Any?,
target: Target?,
dataSource: DataSource?,
isFirstResource: Boolean
): Boolean {
onResourceReady()
return false
}

override fun onLoadFailed(
e: GlideException?,
model: Any?,
target: Target?,
isFirstResource: Boolean
): Boolean {
onLoadFailed(e)
return false
}
})
.into(this)
}

fun ImageView.fitImageGlide(
context: Context,
imageUrl: String,
onResourceReady: () -> Unit,
onLoadFailed: (e: GlideException?) -> Unit
) {
Glide.with(context)
.load(imageUrl)
.centerCrop()
.listener(object : RequestListener {
override fun onResourceReady(
resource: Drawable?,
model: Any?,
target: Target?,
dataSource: DataSource?,
isFirstResource: Boolean
): Boolean {
onResourceReady()
return false
}

override fun onLoadFailed(
e: GlideException?,
model: Any?,
target: Target?,
isFirstResource: Boolean
): Boolean {
onLoadFailed(e)
return false
}
})
.into(this)
}
```
Fragment Code:
```kotlin
Glide.with(context).clear(binding.imageViewBackground)
binding.imageViewBackground.fitGifOrImage(
context,
imageUrl,
onResourceReady = {
// show success state
},
onLoadFailed = {
// show fail state
}
)
```

**Layout XML**:
```xml

...

```

**Stack trace / LogCat**:
N/A no error logs: local debugging logs:
```
2021-09-14 11:32:14.829 32269-32269/***********.stage D/loading chapter: 5
2021-09-14 11:32:15.094 32269-32269/***********.stage D/loading chapter: 6
2021-09-14 11:32:15.198 32269-32269/***********.stage D/success: loaded chapter 6
2021-09-14 11:32:15.914 32269-32269/***********.stage D/success: loaded chapter 5
```

This may be hard to see, but it shows that when two things are being loaded, the first (that should no longer be trying to load) is loaded after the second is showed, therefore showing the incorrect chapter image. (last chapter should be blue not green)

https://user-images.githubusercontent.com/82335757/133319272-5e2e9c35-4568-4f89-ba30-0c39fb5ad5c1.mp4

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.