Prioritise local cache over Network in Coil

Viewed 209

I am using Coil with Jetpack compose. I noticed that in my LazyColumn items the images that load using coil(downloaded from the network) load faster when the device is offline ( this is once the images are cached of course ).

When the device is online, it seems coil is fetching the network copy even though a local cached version of that image exists.

I would like coil to prioritize fetching images based on the following order:

  1. Check if it exists in Memory Cache
  2. If not check if a Disk Cache exists
  3. If the above 2 don't exist then fetch from the network

How do I achieve this? My current ImageRequest Builder looks like this:

ImageRequest.Builder(LocalContext.current)
  .data(downloadUrl)
  .crossfade(true)
  .crossfade(coilFadeDuration)
  .error(R.drawable.black_color_rectangle)
  .placeholder(R.drawable.placholder)
  .fallback(R.drawable.black_color_rectangle)
  .networkCachePolicy(CachePolicy.ENABLED)
  .diskCachePolicy(CachePolicy.ENABLED)
  .memoryCachePolicy(CachePolicy.ENABLED)
  .diskCacheKey(downloadUrl)
  .memoryCacheKey(downloadUrl)
  .diskCacheKey(downloadUrl)
  .memoryCacheKey(downloadUrl)
  .size(Size.ORIGINAL)
  .build()
0 Answers
Related