Xamarin.Forms Android InvalidateCacheEntryAsync doesn't completely remove.
See original GitHub issueXamarin.Forms Android InvalidateCacheEntryAsync can’t completely remove individual cache with CacheKeyFactory.
Description
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.
Steps to Reproduce
- Put a CachedImage specified CacheKeyFactory.
- Specify a StreamImageSource as the source.
- Execute InvalidateCacheEntryAsync(the key, All, true)
- Update the ImageSource.Source to the other source with the same key as previous.
- The new image will be reflected on CachedImage.
- Update it again.
- The previous image will be displayed.
Expected Behavior
Once InvalidateCacheEntryAsync method executes, the cache will be removed and the new image will be reflected when CahcedImage.Source is updated.
Actual Behavior
Each time InvalidateCacheEntryAsync method executes and the source is updated, either the previous image will revive or the new image will be reflected.
Basic Information
- Version with issue: 2.4.4.859
- Last known good version: none
- Platform: Xamarin.Forms 3.4.0.1008975 / Android
Screenshots
-
Initial state.
-
Tap Update.
-
Tap Update again.
Reproduction Link / Code
Work Around
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);
}
}
Related link
Issue Analytics
- State:
- Created 5 years ago
- Comments:5 (1 by maintainers)
Top GitHub Comments
Fixed. Thanks @GZidar 👍
I have created a PR with the change we made to the package that resolved this issue for us.
https://github.com/luberda-molinet/FFImageLoading/pull/1213