On Android with Xamarin.Forms, when using StreamImageSource for CachedImage.Source and setting CacheKeyFactory, InvalidateCacheEntryAsync method can't completely remove individual cache.
On iOS, it can be removed and display the new image.
I use SQLite DB to manage images for my app and the record id is used as the cache key. And then I use the same key after removing the cache when an image is updated.
But the previous image will soon revive and display it.
Once InvalidateCacheEntryAsync method executes, the cache will be removed and the new image will be reflected when CahcedImage.Source is updated.
Each time InvalidateCacheEntryAsync method executes and the source is updated, either the previous image will revive or the new image will be reflected.
Initial state.

Tap Update.

Tap Update again.

It removes the entry also from the cache of the reuse_pool.
public class ImageServiceEx:IImageServiceEx
{
static ByteBoundStrongLruCache<SelfDisposingBitmapDrawable> _reusePool;
static ByteBoundStrongLruCache<SelfDisposingBitmapDrawable> ReusePool
{
get
{
if (_reusePool != null)
return _reusePool;
var imageCache = ImageCache.Instance as ImageCache<SelfDisposingBitmapDrawable>;
var fieldInfo = imageCache.GetType().GetField("_cache", BindingFlags.Instance | BindingFlags.NonPublic);
var innerCache = (ReuseBitmapDrawableCache<SelfDisposingBitmapDrawable>)fieldInfo.GetValue(imageCache);
fieldInfo = innerCache.GetType().GetField("reuse_pool", BindingFlags.Instance | BindingFlags.NonPublic);
_reusePool = (ByteBoundStrongLruCache<SelfDisposingBitmapDrawable>)fieldInfo.GetValue(innerCache);
return _reusePool;
}
}
public async Task ForceInvalidateCacheEntryAsync(string key, CacheType cacheType, bool removeSimilar = false)
{
await ImageService.Instance.InvalidateCacheEntryAsync(key, cacheType, removeSimilar);
ReusePool.Remove(key);
}
}
See also #1127
Happening on Android (not forms) also. The workaround is to remove the image from the ImageViewAsync before invalidating / changing FF cache.
Workaround: imageView.SetImageDrawable(null);
Usecase:
I have created a PR with the change we made to the package that resolved this issue for us.
@GZidar
Thanks!
Fixed. Thanks @GZidar 馃憤
Most helpful comment
Fixed. Thanks @GZidar 馃憤