Skip to content

Commit

Permalink
Improve RequestBuilder.submitAsync() to ensure the coroutine continua…
Browse files Browse the repository at this point in the history
…tion is resumed only once (#4436)

`RequestListener.onResourceReady()` may also be called to provide the
placeholder image when the request is starting or when it is cleared.

Check if the current request is complete (its status set to COMPLETE) to
determine if the Glide resource is for a thumbnail/placeholder or the
final image, and resume the coroutine only for the final image (or in
case of error).
This logic is [borrowed from the Glide Flow
API](https://github.com/bumptech/glide/blob/a7351b0ecf6656ad937fbc52fe9e90d3b289c265/integration/ktx/src/main/java/com/bumptech/glide/integration/ktx/Flows.kt#L378).
  • Loading branch information
cbeyls authored May 12, 2024
1 parent 11d18e1 commit f9221b3
Showing 1 changed file with 5 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import kotlinx.coroutines.suspendCancellableCoroutine
* Allows waiting for a Glide request to complete without blocking a background thread.
*/
suspend fun <R> RequestBuilder<R>.submitAsync(
width: Int = Int.MIN_VALUE,
height: Int = Int.MIN_VALUE
width: Int = Target.SIZE_ORIGINAL,
height: Int = Target.SIZE_ORIGINAL
): R {
return suspendCancellableCoroutine { continuation ->
val target = addListener(
Expand All @@ -36,7 +36,9 @@ suspend fun <R> RequestBuilder<R>.submitAsync(
dataSource: DataSource,
isFirstResource: Boolean
): Boolean {
continuation.resume(resource)
if (target?.request?.isComplete == true) {
continuation.resume(resource)
}
return false
}
}
Expand Down

0 comments on commit f9221b3

Please sign in to comment.