Skip to content

Commit

Permalink
Reload images using coil only once
Browse files Browse the repository at this point in the history
  • Loading branch information
saket committed Apr 28, 2024
1 parent 7a150a5 commit 30b912b
Showing 1 changed file with 12 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ internal class Resolver(
)

override suspend fun work() {
work(forcedMemoryCachePolicy = null)
work(skipMemoryCache = false)
}

private suspend fun work(forcedMemoryCachePolicy: CachePolicy?) {
private suspend fun work(skipMemoryCache: Boolean) {
val result = imageLoader.execute(
request.newBuilder()
.size(request.defined.sizeResolver ?: sizeResolver)
Expand All @@ -103,7 +103,9 @@ internal class Resolver(
CachePolicy.DISABLED -> CachePolicy.WRITE_ONLY
}
)
.memoryCachePolicy(forcedMemoryCachePolicy ?: request.memoryCachePolicy)
.memoryCachePolicy(
if (skipMemoryCache) CachePolicy.WRITE_ONLY else request.memoryCachePolicy
)
// This will unfortunately replace any existing target, but it is also the only
// way to read placeholder images set using ImageRequest#placeholderMemoryCacheKey.
// Placeholder images should be small in size so sub-sampling isn't needed here.
Expand All @@ -129,10 +131,13 @@ internal class Resolver(
null -> null
is EligibleForSubSampling -> it.source
is ImageDeletedOnlyFromDiskCache -> {
println("Image was deleted from the disk cache, but is still present in the memory cache. Reloading request.")
// The app's disk cache was possibly deleted, but the image is
// still cached in memory. Reload the image from the network.
work(forcedMemoryCachePolicy = CachePolicy.WRITE_ONLY)
if (skipMemoryCache) {
error("Coil returned an image that is missing from both its memory and disk caches")
} else {
// The app's disk cache was possibly deleted, but the image is
// still cached in memory. Reload the image from the network.
work(skipMemoryCache = true)
}
return
}
}
Expand Down

0 comments on commit 30b912b

Please sign in to comment.