0

Like in title said I need to update picture when got same URL, this URL whenever is called returns random picture.

Got code that is working when URL is diferent:

var model by remember { mutableStateOf(URL) }
AsyncImage(
    model = model,
    contentDescription = "",
    modifier = Modifier.clickable(onClick = {
        model = newUrl
    })
)

But I what I need is to call same URL that returns random picture:

var model by remember { mutableStateOf(URL) }
AsyncImage(
    model = model,
    contentDescription = "",
    modifier = Modifier.clickable(onClick = {
        model = URL //this is what I need
    })
)

You can check this example:

URL = https://cataas.com/cat you will see random cat every time you refresh page.

Jan Bína
  • 3,447
  • 14
  • 16
acakojic
  • 306
  • 2
  • 11

1 Answers1

1

If you need to reload the image with the same url, you can do something like this in Coil:

var requestId by remember { mutableStateOf(0) }
val imageRequest = ImageRequest.Builder(LocalContext.current)
    .data(data)
    .memoryCachePolicy(CachePolicy.DISABLED)
    .diskCachePolicy(CachePolicy.DISABLED)
    .setParameter("requestId", requestId, memoryCacheKey = null)
    .build()

AsyncImage(
    model = imageRequest,
    contentDescription = "",
    modifier = Modifier.clickable(onClick = {
        requestId++
    })
)

The idea for setParameter comes from this issue: https://github.com/coil-kt/coil/issues/884. Changing the parameter makes coil execute the request again. You also have to disable cache, cause unlike in the issue, request was completed successfully in this case.

Jan Bína
  • 3,447
  • 14
  • 16
  • This code is not working. I will check it on link. – acakojic May 09 '23 at 18:42
  • You are right, the issue works with failed request, we are using that in our app and it works well, and I just assumed it would work for successful requests too. Tried it now and found out you also have to disable the cache. Updated my answer. – Jan Bína May 09 '23 at 21:03
  • 1
    Plus, disabling the cache is a good idea in this example anyway, you don't want to pile up the cat pictures in your memory I guess :D – Jan Bína May 09 '23 at 21:06
  • Yeah I don't need cache in this example :D Don't have experience with caching in memory or disk.. If understand caching if enabled will save picture in memory and it will be lost when I kill the application or what? Is this why cache is important? Please correct me if I'm wrong, trying to understand how caching works. – acakojic May 09 '23 at 22:04
  • 1
    Yeah, coil has two types of caching actually, memory and disk. Memory is for all images, disk only for images downloaded from the internet. You can read something about it here: https://coil-kt.github.io/coil/image_loaders/#caching Once you download an image, it will be saved to the disk cache and you won't have to download it again even after the app is killed. It can only eventually be replaced by other images in the cache if you have lots of them. – Jan Bína May 09 '23 at 22:31
  • 1
    It doesn' make sense in your case because you get different images for the same url, but coil downloads the image the first time, saves it to disk and the second time you request the same url, it just serves it from the disk - and you don't want that. – Jan Bína May 09 '23 at 22:35
  • Now it is more clear what caching is about :-) – acakojic May 09 '23 at 22:56