'How can I get a new image not cached in android coil?
I am developing an android app using the Jetpack Compose with Coil ImageLoader library.
It shows a user's profile image.
I receive the profile data from the API. GET: /users/{userId}
The response contains userId and profileImgKey.
For the user profile image, Backend provides GET: /photo/{userId} API.
But the problem is that if some user update his/her profile image, other users still see the previous profile image not the new image.
Because it is cached by Coil.
If I turn-off the caching option, it may work fine. But I don't want to do it. I don't want to lose the performance benefit.
When the user update their profile image, the profileImgKey is changed.
So I want to use this as a cache key.
But I don't know how to use this.
Solution 1:[1]
The easiest way to ensure that you get a fresh image, even when it changes is to append a query parameter to the url with a random value. For example:
someurl/user1.jpg?49610269
And done like this:
val imageUrl = "someUrl/user1.jpg?" + (0..1_000_000).random()
If your url already has query parameters, just add a new parameter that your url doesn't use.
Images in caches are reused whenever the url (including the query string) does not change. By changing the url, you force Coil to obtain the most recent image.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Johann |
