Search code examples
androidcachinguniversal-image-loaderconcurrentmodification

Universal Image Loader: I get ConcurrentModificationException when trying to remove image from cache


I need to arbitrarily replace a cached image by requesting it again from server.

I'm currently using removeFromCache as follows:

public void loadImage(String url, ImageView view, boolean updateCache){
    if(updateCache){
        MemoryCacheUtil.removeFromCache(url, ImageLoader.getInstance().getMemoryCache());
    }
    ImageLoader.getInstance().displayImage(url, view);
}

The problem is sometimes it throws ConcurrentModificationException.

What's the best way to do it?

  • Can I synchronize my call to remove on the Collection used internally in the library somehow?
  • Does the library give me another way to tell I want to "cache miss" one image arbitrarily?

UIL version is 1.8.4

Stack trace:

FATAL EXCEPTION: main java.util.ConcurrentModificationException at java.util.LinkedHashMap$LinkedHashIterator.nextEntry(LinkedHashMap.java:350) at java.util.LinkedHashMap$KeyIterator.next(LinkedHashMap.java:370) at java.util.HashSet.(HashSet.java:76) at com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache.keys(LruMemoryCache.java:124) at com.nostra13.universalimageloader.core.assist.MemoryCacheUtil.removeFromCache(MemoryCacheUtil.java:102) at uk.frequency.glance.android.util.ImageLoadingManager.loadImage(ImageLoadingManager.java:120)


Solution

  • This is known bug: https://github.com/nostra13/Android-Universal-Image-Loader/issues/265

    Fixed in UIL v1.8.5.